plotly / plotly_express

Plotly Express - Simple syntax for complex charts. Now integrated into plotly.py!
https://plot.ly/python/plotly-express/
MIT License
4 stars 0 forks source link

Subplots using differnet columns? #127

Closed ziliangok closed 5 years ago

ziliangok commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

nicolaskruchten commented 4 years ago

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