折线图和散点图
import plotly.graph_objs as go
import plotly
import numpy as np
# 画线
trace1 = go.Scatter(
x=np.arange(15),
y=[np.sin(i) if i % 5 else None for i in np.arange(15)],
name='Gaps',
)
# 画线,要求对于缺失值,断开
trace2 = go.Scatter(
x=np.arange(15),
y=[np.cos(i) if i % 5 else None for i in np.arange(15)],
name='<b>No</b> Gaps', # 可以使用html风格
connectgaps=True # 对于缺失值,是否跳过去,默认为False
)
# 画散点图,并配置每个点上的 text
trace3 = go.Scatter(
x=np.arange(15),
y=np.sin(0.666 * np.arange(15)),
text=[str(i) + '_text' for i in np.arange(15)], # 可以显示每个点对应的文字
textposition='top center',
mode='markers+text', # markers 代表散点,text代表显示text
name='scatter'
)
# 画散点图,并配置每个点的大小、颜色
trace4 = go.Scatter(
x=np.arange(15),
y=np.cos(0.666 * np.arange(15)),
text=[str(i) + '_text' for i in np.arange(15)], # 可以显示每个点对应的文字
textposition='top center',
marker={'color': ['red', 'blue', 'green'] * 5, 'size': [20, 50, 100] * 5},
mode='markers',
name='scatter2'
)
# layout:配置图表(也可以省略)
layout = go.Layout(title="all plots", xaxis={'title': 'x'}, yaxis={'title': 'value'})
# fig = go.Figure(data=[trace1, trace2, trace3], layout=layout)
fig = go.Figure(data=[trace1, trace2, trace3, trace4])
plotly.offline.plot(fig, filename='example.html')
知识点:
connectgaps=True
折线图的 gap 是中断还是连续- name可以做 html 标记
mode='markers+text'
调整折线还是散点,是否标记 text- marker 传入一个 dict,可以对点的大小、颜色进行调整
Bar
import plotly.graph_objs as go
import plotly
# 原始数据
dataset = {'x': ['Windows', 'Linux', 'Unix', 'MacOS'],
'y1': [45, 26, 37, 13],
'y2': [19, 27, 33, 21]}
# 拆成画图所需数据格式
data1 = go.Bar(
x=dataset['x'],
y=dataset['y1'],
name='v1'
)
data2 = go.Bar(
x=dataset['x'],
y=dataset['y2'],
name='v2'
)
layout = go.Layout(title="bar charts", xaxis={'title': 'x'}, yaxis={'title': 'value'})
fig = go.Figure(data=[data1, data2], layout=layout)
plotly.offline.plot(fig, filename='example.html')
注
- 同样可以使用
marker={'color':['red','blue']}
来配置颜色
填充区域
饼图
import plotly.graph_objs as go
import plotly
dataset = {'labels': ['Windows', 'Linux', 'Unix', 'MacOS', 'Android', 'iOS'],
'values': [280, 25, 10, 100, 250, 270]}
data1 = go.Pie(
labels=dataset['labels'],
values=dataset['values']
)
layout = go.Layout(title="pie charts")
fig = go.Figure(data=[data1], layout=layout)
plotly.offline.plot(fig, filename='example.html')
Heatmap
trace=go.Heatmap(
z=[
[1,2,3,4],
[5,6,7,8]
]
)
plotly.offline.plot([trace])
区域块
trace=go.Scatter(
x=[1,2,6],
y=[1,2,0.5],
fill="tonexty",
fillcolor="#FF0"
)
plotly.offline.plot([trace])
Histogram
import scipy.stats as stats
trace=go.Histogram(
x=stats.norm().rvs(100),
marker=dict(
color=["red","blue","green"]*30
)
)
plotly.offline.plot([trace])
Histogram2d
trace=go.Histogram2d(
x=[1,2,3,3,3,4,5],
y=[1,2,3,3,3,4,5]
)
py.iplot([trace])
地图
地图1
trace=go.Scattergeo(
lon=[100, 400],
lat=[0,0],
marker=dict(
color=['red', 'blue'],
size=[30,50]
),
mode="markers"
)
plotly.offline.plot([trace])
地图2
trace=go.Choropleth(
locations=['AZ','CA','VT'],
locationmode='USA-states',
colorscale='Picnic',
z=[10,20,40]
)
layout=dict(geo=dict(scope='usa'))
fig=go.Figure(data=[trace], layout=layout)
py.iplot(fig)
3D Surface 图
trace=go.Surface(
colorscale='Viridis',
z=[
[3,5,8,13],
[21,13,8,5]
]
)
py.iplot([trace])
3D scatter 图
trace=go.Scatter3d(
x=[9,8,5,1],
y=[1,2,4,8],
z=[11,8,15,3],
mode="lines"
)
py.iplot([trace])
注
mode
可以是’lines’,’markers’,对应三维线、点
Boxplot
trace=go.Box(
x=[1,2,3,3,3,4,5]
)
py.iplot([trace])
基本配置
1
- plotly.offline.plot会生成一个离线的html文件,然后自动打开
- plotly.offline.iplot可以在jupyter内直接生成图标。
配置坐标轴
import plotly.graph_objs as go
import plotly
trace = go.Scatter(
x=[-1, 2, 3, 4],
y=[-1, 2, 3, 6]
)
axis_template = dict(
showgrid=True, # 网格
zeroline=True, # 是否显示基线,即沿着(0,0)画出x轴和y轴
nticks=50, # 显示多少个网格
showline=True, # 显示图表的外窗格线
title='X axis',
mirror='all',
zerolinecolor="#FF0000"
)
layout = go.Layout(xaxis=axis_template, yaxis=axis_template)
fig = go.Figure(data=[trace], layout=layout)
plotly.offline.plot(fig)
参考文献
https://plot.ly/python/
https://blog.csdn.net/fox64194167/article/details/80458711
https://blog.csdn.net/u012897374/article/details/77857980