【问题标题】:Plotly: How to add quivers to subplots?Plotly:如何在子图中添加箭袋?
【发布时间】:2021-01-13 07:13:20
【问题描述】:

我想在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'

谢谢!

【问题讨论】:

  • 当然我试过了,但是我遇到了这个错误:TypeError: add_trace() got an unexpected keyword argument 'data'
  • 所以你想要一个类似于昨天答案的图表,但在子图中分开?
  • 是的,这就是我想要的!
  • @hulyce 我的建议对你有什么效果?

标签: python plotly subplot


【解决方案1】:

使用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)

使用下面的 sn-p 将生成以下具有 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,所以我真的不知道如何在后台管理它,尽管如果只是将字典添加到数据列表中,那么它可能没那么重要
  • 另外,您是如何设法包含标签trace 0trace 1 的?
  • @RichieV 不知道关于内存占用。我只是专注于使用make_subplots() 完成它。无论哪种方式,我认为这并不重要。即使它包含在 Dash 应用程序中。我生产的螺柱方式更笨重,但它仍然运行得很快。
  • @RichieV 关于标签,乍一看可能有点古怪。但据我所知,这是默认行为。一条痕迹 => 没有标签。多个跟踪 => 标签。
【解决方案2】:

我通过手动调整轴而不是使用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 找到更好的解决方案,这似乎应该是首选选项
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 2012-10-02
  • 2013-09-17
  • 2020-05-10
  • 1970-01-01
相关资源
最近更新 更多