Lani
Lani

Reputation: 107

Plotting Python Plotly ECDF subplots with marginal plots

I would like to plot several plots in a subplot, specifically ecdf plots which are found under plotly express. Unfortunately I cannot get it to work because it appears subplot expects a graph objects plotly plot. The error says it receives invalid data, specifically:

"Invalid element(s) received for the 'data' property"

Obviously that means that of the following, ecdf is not included: ['bar', 'barpolar', 'box', 'candlestick', 'carpet', 'choropleth', 'choroplethmapbox', 'cone', 'contour', 'contourcarpet', 'densitymapbox', 'funnel', 'funnelarea', 'heatmap', 'heatmapgl', 'histogram', 'histogram2d', 'histogram2dcontour', 'icicle', 'image', 'indicator', 'isosurface', 'mesh3d', 'ohlc', 'parcats', 'parcoords', 'pie', 'pointcloud', 'sankey', 'scatter', 'scatter3d', 'scattercarpet', 'scattergeo', 'scattergl', 'scattermapbox', 'scatterpolar', 'scatterpolargl', 'scatterternary', 'splom', 'streamtube', 'sunburst', 'surface', 'table', 'treemap', 'violin', 'volume', 'waterfall']

Great, now, is there a work around that will allow me to plot a few of these guys next to each other?

Here's the code for a simple ecdf plot as from the documentation.

import plotly.express as px
df = px.data.tips()
fig = px.ecdf(df, x="total_bill", color="sex", markers=True, lines=False, marginal="histogram")
fig.show()

If I wanted to plot two of this same plot together for example, I would expect the following code (basically copied from the documentation) to work, probably, (if it accepted ecdf) but I cannot get it to work for the aforementioned reasons.

from plotly.subplots import make_subplots
import plotly.graph_objects as go

df = px.data.tips()
fig = make_subplots(rows=1, cols=2)

fig.add_trace(
    px.ecdf(df, x="total_bill", color="sex", markers=True, lines=False, marginal="histogram"),
    row=1, col=1
)

fig.add_trace(
    px.ecdf(df, x="total_bill", color="sex", markers=True, lines=False, marginal="histogram"),
    row=1, col=2
)

fig.update_layout(height=600, width=800, title_text="Side By Side Subplots")
fig.show()

Is there a work around for px.ecdf subplots?

Thank you in advance!

Upvotes: 1

Views: 2539

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31226

  • ECDF plots follow the Plotly Express pattern of having face_col parameter https://plotly.com/python-api-reference/generated/plotly.express.ecdf.html
  • simplest way to achieve this is to prepare the dataframe for this capability. In this example have created two copies of the data with each copy having a column for this capability
  • alternative is far more complex, both make_subplots() and px.ecdf() create multiple x and y axis. It would be necessary to manage all of these yourself
import plotly.express as px
import pandas as pd
df = px.data.tips()
df = pd.concat([px.data.tips().assign(col=c) for c in ["left","right"] ])
fig = px.ecdf(df, x="total_bill", color="sex", markers=True, lines=False, marginal="histogram", facet_col="col")

fig.update_layout(height=600, width=800, title_text="Side By Side Subplots")

enter image description here

Upvotes: 2

Related Questions