Plotly Python Library的学习记录—— Scatter Plots(散点图)

目前正在学习plotly,用来在数据科学领域中用来绘制图标的。官方学习地址:https://plot.ly/python/

Basic Charts包括 :

Scatter Plots(散点图),

Line Charts(折线图 ),

Bar Charts(条形图 ),

Pie Charts(饼图),

More Basic Charts(其他基本图表)。

学习案例均来自https://plot.ly/python/中,在此仅对学习的案例进行记录学习。

安装方面:

我在Ubuntu16.04中安装anaconda后,发现pip和pip3都不能正常使用。

解决:一.使用conda新建一个虚拟环境,在里面安装plotly.然后可以使用ipython来学习plotly。

           二.继续采用pip安装。直接pip install plotly会报错,根据报错提示,pip install plotly --user后能正常安装,plotly被安装  在./anaconda3/lib/python3.6/site-packages 下(因为安装完anaconda后pip也被置换为anaconda 中的pip了)。可以采用ipython或者jupyter notebook来学习plotly。

注意:##代表学习中的注释笔记

==================================================================================================

Scatter Plots

例一:
import plotly.plotly as py
import plotly.graph_objs as go

# Create random data with numpy
import numpy as np

N = 1000
random_x = np.random.randn(N)  ##从标准正态分布中返回1000个样本值
random_y = np.random.randn(N)

# Create a trace
trace = go.Scatter(
    x = random_x,
    y = random_y,
    mode = 'markers'  ##makers代表标记,即散点
)

data = [trace]

# Plot and embed in ipython notebook!
py.iplot(data, filename='basic-scatter')   ##在jupyter notebook中 中展示出散点图。

# or plot with: plot_url = py.plot(data, filename='basic-line')   ##以html的形式展示。
============================================================================================================================
例二:
import plotly.plotly as py
import plotly.graph_objs as go

# Create random data with numpy
import numpy as np

N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N)+5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-5

# Create traces
trace0 = go.Scatter(
    x = random_x,
    y = random_y0,
    mode = 'markers',
    name = 'markers'
)
trace1 = go.Scatter(
    x = random_x,
    y = random_y1,
    mode = 'lines+markers',   ##表示以标记+折线的形式展示。
    name = 'lines+markers'
)
trace2 = go.Scatter(
    x = random_x,
    y = random_y2,
    mode = 'lines',     ##仅以折线的形式展示
    name = 'lines' 
)

data = [trace0, trace1, trace2]
py.iplot(data, filename='scatter-mode')
==============================================================================================================================
例三:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

N = 500

trace0 = go.Scatter(
    x = np.random.randn(N),
    y = np.random.randn(N)+2,
    name = 'Above',
    mode = 'markers',
    marker = dict(                        ##对marker进行样式的设计,比如size,color,line(标记的边框线)。
        size = 10,
        color = 'rgba(152, 0, 0, .8)',
        line = dict(
            width = 2,
            color = 'rgb(0, 0, 0)'
        )
    )
)

trace1 = go.Scatter(
    x = np.random.randn(N),
    y = np.random.randn(N)-2,
    name = 'Below',
    mode = 'markers',
    marker = dict(
        size = 10,
        color = 'rgba(255, 182, 193, .9)',
        line = dict(
            width = 2,
        )
    )
)

data = [trace0, trace1]

layout = dict(title = 'Styled Scatter',         ##图标的名称
              yaxis = dict(zeroline = False),   ##表示不显示x轴的坐标
              xaxis = dict(zeroline = False)    ##表示不显示y轴的坐标
             )

fig = dict(data=data, layout=layout)
py.iplot(fig, filename='styled-scatter')
===============================================================================================================================
例四:
import plotly.plotly as py
import plotly.graph_objs as go
import random
import numpy as np
import pandas as pd

y = []
traces = []
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv")
# Setting colors for plot.
N = 53
c = ['hsl('+str(h)+',50%'+',50%)' for h in np.linspace(0, 360, N)]

for i in range(int(N)):
    y.append((2000+i))
    trace0=go.Scatter(
        x=df['Rank'],
        y=df['Population']+(i*1000000),
        mode='markers',
        marker=dict(size=14,
                    line=dict(width=1),
                    color=c[i],
                    opacity=0.3
                   ),
        name=y[i],
        text= df['State']) # The hover text goes here... 
    traces.append(trace0);

layout= go.Layout(
    title= 'Stats of USA States',
    hovermode= 'closest',         ##鼠标悬停在图标上显示数据的形式,还有“x”,"y","False"
    xaxis= dict(
        title= 'Population',
        ticklen= 5,               ##坐标刻度数据边“——”的长度
        zeroline= False,
        gridwidth= 2,             ##图标网络格子的宽度
    ),
    yaxis=dict(
        title='Rank',
        ticklen=5,
        gridwidth=2,
    ),
    showlegend=False              ##图例的对照表
)
fig = go.Figure(data=traces, layout=layout)
py.iplot(fig, filename='scatter_hover_labels')
===============================================================================================================================
例五:
import plotly.graph_objs as go
import plotly.plotly as py

import numpy as np

trace1 = go.Scatter(
    y = np.random.randn(500),
    mode='markers',
    marker=dict(
        size=16,
        color = np.random.randn(500), #set color equal to a variable
        colorscale='Viridis',  ##调色盘名称
        showscale=True
    )
)
data = [trace1]

py.iplot(data, filename='scatter-plot-with-colorscale')

你可能感兴趣的:(Plotly Python Library的学习记录—— Scatter Plots(散点图))