[英]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
不接受 data 參數,並且add_traces
不接受 col 和 row 參數。
啟動這段代碼時,我收到以下錯誤:
TypeError: add_trace() got an unexpected keyword argument 'data'
謝謝!
似乎完整的圖形對象不能用於使用make_subplots()
設置子圖。 正如您所展示的, fig.add_trace()
不能直接用於ff.create_quiver()
圖形中的數據。 什么會的工作,雖然,是包括獨特的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]
這聽起來可能有點復雜,但實際上一點也不復雜。 只需制作兩個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)
使用下面的代碼段將生成以下子圖設置,其中包含 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()
我通過手動調整軸而不是使用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.