简体   繁体   English

带有情节表达的子图 4

[英]subplots with plotly express 4

I have the following, using plotly express:我有以下内容,使用 plotly express:

fig = px.line(series1, x='timestamp', y='data')
fig.show()

and it works properly.它工作正常。

I want to make multiple plots together, so I did:我想一起制作多个图,所以我做了:

fig = make_subplots(rows=2, cols=1)

fig.add_trace(px.line(series1, x='timestamp', y='data'), row=1, col=1)
fig.add_trace(px.line(series2, x='timestamp', y='data'), row=1, col=1)

fig.add_trace(px.line(series2, x='timestamp', y='data'), row=2, col=1)

fig.show()

but I get:但我得到:

Invalid element(s) received for the 'data' property of Invalid elements include: [Figure({ 'data': [{'hoverlabel': {'namelength': 0},为无效元素的 'data' 属性接收到的无效元素包括: [Figure({ 'data': [{'hoverlabel': {'namelength': 0},

although,虽然,

fig = make_subplots(rows=1, cols=2)

fig.add_trace(go.Scatter(x=series1['timestamp'], y=series1['data']), row=1, col=1)
fig.add_trace(go.Scatter(x=series2['timestamp'], y=series2['data']), row=1, col=1)
fig.add_trace(go.Scatter(x=series2['timestamp'], y=series2['data']), row=1, col=2)

fig.show()

will work;将工作; so it looks like plotly express doesn't work with subplots.所以看起来 plotly express 不适用于子图。

did I miss anything?我错过了什么吗?

additionally, as a bonus question: I haven't found how I can specify the color for each of the traces.另外,作为一个额外的问题:我还没有找到如何为每个跟踪指定颜色。

Plotly Express (PX) functions like px.line() return figures, just like make_subplots does.px.line()这样的 Plotly Express (PX) 函数返回数字,就像make_subplots一样。 On the other hand, add_trace() accepts trace objects and not figure objects, which is why fig.add_trace(px.line()) doesn't work.另一方面, add_trace()接受跟踪对象而不是图形对象,这就是fig.add_trace(px.line())不起作用的原因。

PX can make subplots using make_subplots internally if you pass in the facet_row and/or facet_col arguments.如果您传入facet_row和/或facet_col参数,PX 可以在内部使用make_subplots制作子图。 This means that it is compatible with make_subplots in that you can call add_trace(row=r, col=c) on a figure created via px.whatever() .这意味着它与make_subplots兼容,因为您可以在通过px.whatever()创建的图形上调用add_trace(row=r, col=c) px.whatever()

In the spirit of StackOverflow, I recommend splitting your "bonus question" into a separate SO question.本着 StackOverflow 的精神,我建议将您的“奖金问题”拆分为一个单独的 SO 问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM