Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support list inputs for plotly express x, y arguments #2224

Closed
samjett247 opened this issue Feb 24, 2020 · 8 comments
Closed

Comments

@samjett247
Copy link

First off, thanks for the awesome plotly.express package, its fulfilling a great need for interactive plotting capabilities.

One problem I've encountered frequently is that I have variables I want to plot in separate columns in my dataframe, and I have to make a subplot to plot the distributions or correlations of all of these variables. For these purposes, it would be very helpful if px.scatter, px.box , and the other px methods could accept the lists of column names as x or y variables. Note that this is different from faceting by column or row, because for the facet: (a) the yaxes and/or xaxes are typically shared, and (b) the "column" or row to be faceted on is a single column. Would love to hear the communities thoughts about this idea. Example code below.

import plotly.express as px
from plotly.subplots import make_subplots
import pandas as pd

temp = [
    {"Clinic": "A", "Subject": "Bill", "Height(cm)": 182, "Weight(kg)": 101, "BloodPressureHigh": 128, "BloodPressureLow": 90},
    {"Clinic": "A", "Subject": "Susie", "Height(cm)": 142, "Weight(kg)": 67, "BloodPressureHigh": 120, "BloodPressureLow": 70},
    {"Clinic": "B", "Subject": "John", "Height(cm)": 202, "Weight(kg)": 89, "BloodPressureHigh": 118, "BloodPressureLow": 85},
    {"Clinic": "B", "Subject": "Stacy", "Height(cm)": 156, "Weight(kg)": 78, "BloodPressureHigh": 114, "BloodPressureLow": 76},
    {"Clinic": "B", "Subject": "Lisa", "Height(cm)": 164, "Weight(kg)": 59, "BloodPressureHigh": 112, "BloodPressureLow": 74} 
]
df = pd.DataFrame(temp)
# This is the call I want to be able to make
# px.box(data_frame=df, x='Clinic', y=['Height(cm)', 'Weight(kg)', "BloodPressureHigh", "BloodPressureLow"]) 

# Instead, this is what I have to do
fig = make_subplots(rows=1, cols=4)
for j,y in enumerate(['Height(cm)', 'Weight(kg)', "BloodPressureHigh", "BloodPressureLow"]):
    localfig = px.box(data_frame=df, x='Clinic', y=y) 
    # Or the below if you want to plot scatter
    # localfig = px.scatter(data_frame=df, x='Height(cm)', y=y)
    trace1 = localfig['data'][0]
    fig.add_trace(trace1, row=1, col=j+1)
fig.show()
@nicolaskruchten

This comment has been minimized.

@samjett247
Copy link
Author

Thanks @nicolaskruchten for the helpful example - just what I needed!

@nicolaskruchten
Copy link
Contributor

We've actually gone ahead an implemented this feature after all! https://medium.com/plotly/beyond-tidy-plotly-express-now-accepts-wide-form-and-mixed-form-data-bdc3e054f891

@eisenlohr
Copy link

Support for wide format is very nice to have. However, what I am struggling with is the issue that a px.line plot connects traces resulting from different columns as demonstrated in this MWE:

df = pd.DataFrame(data={'who':['a','a','b','b'],
                        'x':[0,1,0,1],
                        'score':[1.0,2,3,4],
                        'miss':[3.2,2.5,1.3,1.5],
                        })
px.line(df,x='x',y=['miss','score'],color='who',width=300,height=300).show()

newplot

My hope/expectation was that above code results in the union of below two separate plots, i.e. without connecting the data across different columns

px.line(df,x='x',y=['miss'],color='who',width=300,height=300).show()
px.line(df,x='x',y=['score'],color='who',width=300,height=300).show()

newplot(1)
newplot(2)

@nicolaskruchten
Copy link
Contributor

nicolaskruchten commented Jun 16, 2020

You can resolve this by adding line_group="variable", to get the lines to be split by variable as well as by color.

df = pd.DataFrame(data={'who':['a','a','b','b'],
                        'x':[0,1,0,1],
                        'score':[1.0,2,3,4],
                        'miss':[3.2,2.5,1.3,1.5],
                        })
px.line(df,x='x',y=['miss','score'], color='who', line_group="variable", width=300,height=300).show()

image

@nicolaskruchten
Copy link
Contributor

The default value of color is "variable" so you get this effect automatically, but if you remap color to something else and you want to maintain the line-grouping, you'll need to make this explicit.

@nicolaskruchten
Copy link
Contributor

Perhaps this should be the default :)

@nicolaskruchten
Copy link
Contributor

@eisenlohr as of 4.8.2, line_group="variable" by default for px.line :)

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

No branches or pull requests

3 participants