简体   繁体   English

Plotly Express 和 Python 上的变量 ID 子图

[英]Subplots by variable ID on Plotly Express with Python

I have plot like this:我有这样的 plot :

var_names = [i for i in test_df.variable.unique()]
var_names.sort()

fig = px.scatter(test_df, x='date', y='value', color='variable', category_orders={'variable': var_names},
                template="ggplot2", title='Median of access',
                 labels={'value': 'Median of access on the week', 'date': 'Week'}
                 ).update_traces(mode='markers+lines')
fig.show()

And my dataframe is:而我的 dataframe 是:

test_df = pd.DataFrame({
        'ID':list('aabdee'),
        'date':['2020-03-22', '2020-03-22', '2020-03-29', '2020-03-22','2020-03-22', '2020-03-29'],
         'B':[1,0.5,4,5,5,4],
         'C':[7,8,9,4,2,3],
         'D':[1,3,1,7,1,1],
         'E':[5,3,6,9,2,4]
})
test_df = test_df.melt(['ID', 'date']).  

I want to run the code just once and build different subplots depending on the ID, like if each graph in the picture below were corresponding to the variables of a specific ID.我想只运行一次代码并根据 ID 构建不同的子图,就像下图中的每个图表都对应于特定 ID 的变量。
I tried with 'add trace' as is on the doc, but if I do this I have to copy and paste the code each time filtering by the ID.我尝试使用文档上的“添加跟踪”,但如果我这样做,我必须在每次按 ID 过滤时复制并粘贴代码。

在此处输入图像描述 Thank you a lot!十分感谢!

In the end was super simple, the problem is that the documentation of plotly express is not so easy to find.最后超级简单,问题是plotly express的文档不是那么好找的。

So, you can put the variable facet_col='ID' on your px.scatter(...) and it works:).因此,您可以将变量 facet_col='ID' 放在 px.scatter(...) 上,它可以工作:)。

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

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