plotly中的graph_objs是plotly下的子模块,用于导入plotly中所有图形对象。在根据绘图需求从graph_objs中导入相应的obj之后,接下来需要做的事情是基于待展示的数据,为指定的obj配置相关参数,这在plotly中称为构造traces(create traces)。
1、Import graph_objs as go
2、构造traces:
x : x轴
y : y轴
mode : plot的类型(如marker, line , line + markers)
name : plot的名称
marker : 定义点的性质,字典型
color : 线的颜色,包括RGB(红,绿,蓝)和不透明度(alpha)
text : 悬停文本
3、data : 保存trace的list
4、定义layout : 布局,字典型
title : 图像的主标题
x axis : 字典型
title : x轴标题
ticklen : x轴刻度线的长度
zeroline : 是否显示零线
5、fig : 将graph部分和layout部分组合成figure对象
6、iplot() : 绘制由data和layout创建的图
In [1]:
# 初次运行尝试
import plotly.graph_objects as go
fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
fig.show()
In [3]:
#散点图、折线图
# Create random data with numpy
import numpy as np
np.random.seed(1)
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
fig = go.Figure()
# Add traces
fig.add_trace(go.Scatter(x=random_x, y=random_y0,
mode='markers',
name='markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y1,
mode='lines+markers',
name='lines+markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y2,
mode='lines',
name='lines'))
fig.show()
In [4]:
# 饼图
import plotly.graph_objects as go
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500, 2500, 1053, 500]
fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
fig.show()
In [6]:
# 等高线
import plotly.graph_objects as go
fig = go.Figure(data =
go.Contour(
z=[[10, 10.625, 12.5, 15.625, 20],
[5.625, 6.25, 8.125, 11.25, 15.625],
[2.5, 3.125, 5., 8.125, 12.5],
[0.625, 1.25, 3.125, 6.25, 10.625],
[0, 0.625, 2.5, 5.625, 10]],
x=[-9, -6, -5 , -3, -1], # horizontal axis
y=[0, 1, 4, 5, 7] # vertical axis
))
fig.show()
In [ ]: