Why are subplots made complicated?

I started using plotly instead of matplotlib and seaborn recently. So far my experience has been very positive. However, one annoying thing is the way subplots are created. For example I need to do:

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

fig = make_subplots(rows=1, cols=2)

fig.add_trace(
    go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
    row=1, col=1
)

fig.add_trace(
    go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
    row=1, col=2
)

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

While there is nothing wrong with this, I think it’s rather annoying that now I have to use these go plot instead of regular plots.

What is the reason behind this? Why would it simply not keep the original syntax? It can be quite frustrating. For example, if my single plot code looks like:

px.scatter(range(10), range(10))

And if I wanted to put this on a subplot I need to:

  • use go object and remember to use capital S: go.Scatter
  • now give arrays either as a dict or named args x and y
  • change my code from a range to a list, numpy, array or pandas etc.

Now multiply this by a number of different plotting options: line, scatter, histogram etc and you get to either memorize a huge number of functions that do the same thing or you will keep getting errors and wasting time on googling the right one.

Additionally, in matplotlib it is quite useful to be able to create an axis – ax and then pass it when creating plots which then puts all the plots on that axis. This works quite well with pandas plots where you can easily put two plots from different dataframes on the same axis. Is anything similar possible here?

2 Likes

I agree. subplots in plotly is a nightmare.

@chanansh it does not have to be . . . you can make subplots without using make_subplots just use the facet_col or facet_row param.

df = pd.DataFrame({'x': [1,2,3,20,30,40],
                   'y': [4,5,6,50,60,70],
                   'grp': list('AAABBB')})


fig = px.line(df, x='x', y='y', facet_col='grp', color='grp')
fig.update_yaxes(matches=None, showticklabels=True)
fig.update_xaxes(matches=None)

Thanks for the feedback! For what it’s worth, you can also pass Plotly Express charts to add_trace, for example:

fig.add_trace(
    px.scatter(x=[20, 30, 40], y=[50, 60, 70]),
    row=1, col=2
)

This way you aren’t required to use the Graph Objects API.

1 Like

We have an open feature request/discussion on improving combining multiple charts that folks ITT might be interested in reading: PX figure overlay / layering Β· Issue #2648 Β· plotly/plotly.py Β· GitHub

Would love your feedback there as well!

1 Like