Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Subplots using differnet columns? #127

Closed
ziliangok opened this issue Aug 1, 2019 · 4 comments
Closed

Subplots using differnet columns? #127

ziliangok opened this issue Aug 1, 2019 · 4 comments

Comments

@ziliangok
Copy link

ziliangok commented Aug 1, 2019

For example:
plot1: px.box(df, y='feature1')
plot2: px.box(df, y='feature2')
The facet_row and facet_col can make subplots on one columns, but I want to make subplots on differents columns.

@nicolaskruchten
Copy link
Contributor

Plotly Express operates on "tidy data" only, so you will need to reshape your data frame before passing it into px.box. This is pretty easy with the melt() function in Pandas:

import pandas as pd
df = pd.read_csv(pd.compat.StringIO("""
PlantID,A,B,C,D
1,0,1,2,4
1,3,0,2,0
3,0,0,0,1
4,0,1,1,5
"""))
print(df.head())
   PlantID  A  B  C  D
0        1  0  1  2  4
1        1  3  0  2  0
2        3  0  0  0  1
3        4  0  1  1  5
print(df.melt(id_vars=["PlantID"]).head())
   PlantID variable  value
0        1        A      0
1        1        A      3
2        3        A      0
3        4        A      0
4        1        B      1

Meaning that you can do:

import plotly.express as px
px.box(df.melt(id_vars=["PlantID"]), x="variable", y="value")

image

@nicolaskruchten
Copy link
Contributor

Ah sorry, if you want facets per original column it's just:

import plotly.express as px
px.box(df.melt(id_vars=["PlantID"]), y="value", facet_col="variable", boxmode="overlay")

image

@ziliangok
Copy link
Author

Thank you! Although this is not what I want, it solve my problems. (●^o^●)

@nicolaskruchten
Copy link
Contributor

We've actually implemented support for subplots per columns! https://medium.com/plotly/beyond-tidy-plotly-express-now-accepts-wide-form-and-mixed-form-data-bdc3e054f891

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants