[英]Multiple Subplots & Multiple Colors in Plotly Express
I am having trouble creating subplots in plotly given a pandas data frame with multiple colors.鉴于 pandas 数据框和多个 colors,我无法在 plotly 中创建子图。
Here is an incomplete example creating the individual plots:这是一个创建单个图的不完整示例:
import plotly
df = plotly.express.data.iris()
plot1 = plotly.express.scatter(df, x="sepal_width", y="sepal_length", color="species")
plot2 = plotly.express.scatter(df, x="petal_width", y="petal_length", color="species")
plot1.show(), plot2.show()
From what I read, something like this makes sense but does not work:从我读到的内容来看,这样的事情是有道理的,但不起作用:
import plotly
df = plotly.express.data.iris()
plot1 = plotly.express.scatter(df, x="sepal_width", y="sepal_length", color="species")
plot2 = plotly.express.scatter(df, x="petal_width", y="petal_length", color="species")
fig = plotly.subplots.make_subplots(rows=1, cols=2)
fig.append_trace(plot1, row=1, col=1)
fig.append_trace(plot2, row=1, col=2)
fig.show()
Looking into this, others seem to resolve this issue with a similar setup:看看这个,其他人似乎用类似的设置解决了这个问题:
https://stackoverflow.com/a/65555470/4700548 https://stackoverflow.com/a/65555470/4700548
r-beginners gives a good example below of how to make multiple subplots with one color, but this causes performance issues with many colors. r-beginners 在下面给出了一个很好的例子,说明如何用一种颜色制作多个子图,但这会导致许多 colors 的性能问题。
What is it that I am missing in these examples?我在这些示例中缺少什么?
Edit : Added color to example.编辑:为示例添加颜色。 Added incomplete example.添加了不完整的示例。
Why it can't be done as per the linked example is probably because px and go have different internal data.为什么不能按照链接示例完成可能是因为 px 和 go 具有不同的内部数据。 I have not confirmed this.我还没有证实这一点。 The point I modified is that the data can be specified in this format as plotl1.data[0] to get the scatterplot data.我修改的一点是数据可以指定为plotl1.data[0]这种格式来得到散点图数据。
import plotly.express as px
from plotly.subplots import make_subplots
df = px.data.iris()
plot1 = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
plot2 = px.scatter(df, x="petal_width", y="petal_length", color="species")
#print(plot1['data'][0],plot1['data'][1],plot1['data'][2], end='\n')
fig = make_subplots(rows=1, cols=2, specs=[[{"type": "scatter"}, {"type": "scatter"}]])
fig.append_trace(plot1['data'][0], row=1, col=1)
fig.append_trace(plot1['data'][1], row=1, col=1)
fig.append_trace(plot1['data'][2], row=1, col=1)
fig.append_trace(plot2['data'][0], row=1, col=2)
fig.append_trace(plot2['data'][1], row=1, col=2)
fig.append_trace(plot2['data'][2], row=1, col=2)
fig.update_layout(showlegend=False)
fig.show()
I think my example was rather poor, but r-beginners gives the best solution as the question is posed.我认为我的例子很差,但是 r-beginners 在提出问题时给出了最好的解决方案。
The data I am working with has the same x and y axis labels.我正在处理的数据具有相同的 x 和 y 轴标签。 So, if there are n data frames, you can take advantage of that fact by adding a facet_col and concatenating them.因此,如果有 n 个数据帧,您可以通过添加 facet_col 并将它们连接起来来利用这一事实。
embed_df = pd.concat([
df1,
df2,
df3,
df4,
....
])
fig = plotly.express.scatter(
embed_df,
x = "dim1",
y = "dim2",
color="label",
facet_col="name")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.