Plotly:如何向子图添加箭袋?
Plotly: How to add quivers to subplots?
我想在 plotly
子图中添加箭袋。但是我找不到任何方法来做到这一点。
她是我想做的事情:
fig = plotly.subplots.make_subplots(rows=1, cols=2)
x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y
quivers = ff.create_quiver(x, y, u, v)
fig.add_trace(data=quivers.data,col=1,row=1)
问题是 add_trace
不接受数据参数,add_traces
不接受列和行参数。
启动这段代码时,出现以下错误:
TypeError: add_trace() got an unexpected keyword argument 'data'
谢谢!
我通过手动调整轴而不是使用 make_subplots
找到了 solution in the plotly webpage。
import numpy as np
import plotly.figure_factory as ff
import plotly.graph_objects as go
x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y
fig1 = ff.create_quiver(x, y, u, v, name='trace1')
fig2 = ff.create_quiver(x, y, u*0.9, v*2, name='trace2')
for i in range(len(fig1.data)):
fig1.data[i].xaxis = 'x1'
fig1.data[i].yaxis = 'y1'
for i in range(len(fig2.data)):
fig2.data[i].xaxis = 'x2'
fig2.data[i].yaxis = 'y2'
fig1.layout.xaxis1.update({'anchor': 'y1', 'domain': [0.55, 1]})
# apparently [0.55, 1] is relative to the full chart's dimensions
fig1.layout.yaxis1.update({'anchor': 'x1'})
fig2['layout']['xaxis2'] = {'anchor': 'y2', 'domain': [0, 0.45]}
fig2['layout']['yaxis2'] = {'anchor': 'x2'}
fig = go.Figure()
fig.add_traces([fig1.data[0], fig2.data[0]])
fig.layout.update(fig1.layout)
fig.layout.update(fig2.layout)
看来完整的图形对象不能用于使用 make_subplots()
设置子图。正如您自己所展示的,fig.add_trace()
不能直接用于 ff.create_quiver()
图形中的数据。但是, 的作用是为 fig1.data
:
中的每个 x 和 y 元素包含一个唯一的 go.Scatter
'x': [0.0, 0.0, None, ..., 1.7591036229552444, 1.7526465527333175, None],
'y': [0.0, 0.0, None, ..., 1.9752925735580753, 1.9216800167812427, None]
这听起来可能有点复杂,但实际上一点也不复杂。只需制作两个 ff.create_quiver() figures
并为每个人使用:
# add all fig1.data as individual traces in fig at row=1, col=1
for d in fig1.data:
subplots.add_trace(go.Scatter(x=d['x'], y=d['y']),
row=1, col=1)
使用下面的代码片段将生成以下具有 1 行和 2 列的子图设置。甚至包括所有线条的箭头形状。
情节
完整代码
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.figure_factory as ff
# data
x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y
# quiver plots
fig1 = ff.create_quiver(x, y, u, v)
fig2 = ff.create_quiver(x, y, u*0.9, v*1.1)
# subplot setup
subplots = make_subplots(rows=1, cols=2)
# add all fig1.data as individual traces in fig at row=1, col=1
for d in fig1.data:
subplots.add_trace(go.Scatter(x=d['x'], y=d['y']),
row=1, col=1)
# add all fig2.data as individual traces in fig at row=1, col=2
for d in fig1.data:
subplots.add_trace(go.Scatter(x=d['x'], y=d['y']),
row=1, col=2)
subplots.show()
我想在 plotly
子图中添加箭袋。但是我找不到任何方法来做到这一点。
她是我想做的事情:
fig = plotly.subplots.make_subplots(rows=1, cols=2)
x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y
quivers = ff.create_quiver(x, y, u, v)
fig.add_trace(data=quivers.data,col=1,row=1)
问题是 add_trace
不接受数据参数,add_traces
不接受列和行参数。
启动这段代码时,出现以下错误:
TypeError: add_trace() got an unexpected keyword argument 'data'
谢谢!
我通过手动调整轴而不是使用 make_subplots
找到了 solution in the plotly webpage。
import numpy as np
import plotly.figure_factory as ff
import plotly.graph_objects as go
x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y
fig1 = ff.create_quiver(x, y, u, v, name='trace1')
fig2 = ff.create_quiver(x, y, u*0.9, v*2, name='trace2')
for i in range(len(fig1.data)):
fig1.data[i].xaxis = 'x1'
fig1.data[i].yaxis = 'y1'
for i in range(len(fig2.data)):
fig2.data[i].xaxis = 'x2'
fig2.data[i].yaxis = 'y2'
fig1.layout.xaxis1.update({'anchor': 'y1', 'domain': [0.55, 1]})
# apparently [0.55, 1] is relative to the full chart's dimensions
fig1.layout.yaxis1.update({'anchor': 'x1'})
fig2['layout']['xaxis2'] = {'anchor': 'y2', 'domain': [0, 0.45]}
fig2['layout']['yaxis2'] = {'anchor': 'x2'}
fig = go.Figure()
fig.add_traces([fig1.data[0], fig2.data[0]])
fig.layout.update(fig1.layout)
fig.layout.update(fig2.layout)
看来完整的图形对象不能用于使用 make_subplots()
设置子图。正如您自己所展示的,fig.add_trace()
不能直接用于 ff.create_quiver()
图形中的数据。但是, 的作用是为 fig1.data
:
go.Scatter
'x': [0.0, 0.0, None, ..., 1.7591036229552444, 1.7526465527333175, None],
'y': [0.0, 0.0, None, ..., 1.9752925735580753, 1.9216800167812427, None]
这听起来可能有点复杂,但实际上一点也不复杂。只需制作两个 ff.create_quiver() figures
并为每个人使用:
# add all fig1.data as individual traces in fig at row=1, col=1
for d in fig1.data:
subplots.add_trace(go.Scatter(x=d['x'], y=d['y']),
row=1, col=1)
使用下面的代码片段将生成以下具有 1 行和 2 列的子图设置。甚至包括所有线条的箭头形状。
情节
完整代码
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.figure_factory as ff
# data
x,y = np.meshgrid(np.arange(0, 2, .2), np.arange(0, 2, .2))
u = np.cos(x)*y
v = np.sin(x)*y
# quiver plots
fig1 = ff.create_quiver(x, y, u, v)
fig2 = ff.create_quiver(x, y, u*0.9, v*1.1)
# subplot setup
subplots = make_subplots(rows=1, cols=2)
# add all fig1.data as individual traces in fig at row=1, col=1
for d in fig1.data:
subplots.add_trace(go.Scatter(x=d['x'], y=d['y']),
row=1, col=1)
# add all fig2.data as individual traces in fig at row=1, col=2
for d in fig1.data:
subplots.add_trace(go.Scatter(x=d['x'], y=d['y']),
row=1, col=2)
subplots.show()