[英]Plotly: How to add quivers to subplots?
I'd like to add quivers to a plotly
subplot.我想将plotly
添加到一个情节情节。 But I can't find any way to do this.但我找不到任何方法来做到这一点。
Her's what I'd like to do:她是我想做的:
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)
The problem is add_trace
doesn't accept the data argument, and add_traces
doesn't accept col and row arguments.问题是add_trace
不接受 data 参数,并且add_traces
不接受 col 和 row 参数。
When launching this piece of code, I get the following error:启动这段代码时,我收到以下错误:
TypeError: add_trace() got an unexpected keyword argument 'data'
Thanks!谢谢!
It appears that complete figure objects can not be used to set up subplots using make_subplots()
.似乎完整的图形对象不能用于使用make_subplots()
设置子图。 And as you've shown yourself, fig.add_trace()
can't be used directly with the data from a ff.create_quiver()
figure.正如您所展示的, fig.add_trace()
不能直接用于ff.create_quiver()
图形中的数据。 What will work though, is to include a unique go.Scatter
for each and every x and y element in fig1.data
:什么会的工作,虽然,是包括独特的go.Scatter
每个并在每个x和y元素fig1.data
:
'x': [0.0, 0.0, None, ..., 1.7591036229552444, 1.7526465527333175, None],
'y': [0.0, 0.0, None, ..., 1.9752925735580753, 1.9216800167812427, None]
That may sound a little complicated, but really isn't at all.这听起来可能有点复杂,但实际上一点也不复杂。 Just make two ff.create_quiver() figures
and use this for each of them:只需制作两个ff.create_quiver() figures
并将其用于每个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)
Using the snippet below will produce the following subplot setup with 1 row and two columns.使用下面的代码段将生成以下子图设置,其中包含 1 行和 2 列。 Even the arrow shapes for all lines are included.甚至包括所有线条的箭头形状。
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()
I found a solution in the plotly webpage by manually adjusting the axes instead of using make_subplots
.我通过手动调整轴而不是使用make_subplots
在 plotly 网页中找到了一个解决方案。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.