Hi,
I can't manage to get subplots using the FigureWidget tool which is really powerful thanks to version 3.0. I can't add traces using fig.add_trace( ) because I can't create the subplots before that. Could someone give simple example that works ?
trace0 = go.Scatter(
x=[1, 2, 3],
y=[2, 3, 4])
trace1 = go.Scatter(
x=[20, 30, 40],
y=[5, 5, 5])
trace2 = go.Scatter(
x=[2, 3, 4],
y=[600, 700, 800])
fig = go.FigureWidget(
layout = dict(
title = 'Subplots try'))
fig.add_trace(trace0)
fig.add_trace(trace1)
fig.add_trace(trace2)
# This will raise an error
#fig.append_trace(trace0, 1, 1)
#fig.append_trace(trace1, 1, 2)
#fig.append_trace(trace2, 2, 1)
fig
PS: sorry for code insertion, I struggled with it and I had to insert each line in a code block...
Hi @LePuppy ,
To make subplots you need to first construct a figure with plotly.tools.make_subplots (See https://plot.ly/python/subplots/). This will return a (non-widget) Figure instance, but you can convert it to a FigureWidget like this:
import plotly.graph_objs as go
from plotly.tools import make_subplots
fig = go.FigureWidget(make_subplots(rows=1, cols=2))
Then add traces using add_trace with the row and col parameters. (append_trace still works but add_trace trace is more flexible so I'd recommend using that).
fig.add_trace(trace0, row=1, col=2)
Hope that helps!
This is a good reminder that we need to add a helpful error message when someone tries to add a trace to a subplot without creating the figure using make_subplots. The error message should have explained to @LePuppy what they needed to do (including the FigureWidget bit).
Thank you @jonmmease , this works. While we're at it, how to add some arguments related to the make_subplots function ?
Adding titles to axes works with:
fig.layout.xaxis.update(title='xaxis title')
fig.layout.xaxis2.update(title='xaxis2 title')
fig.layout.yaxis.update(title='yaxis title')
fig.layout.yaxis2.update(title='yaxis2 title')
Or modifying the figure's dimensions:
fig.layout.update(height=800, width=800,
title='Main figure title')
But how about adding titles to subplots or having subplots with shared axes ?
Without the FigureWidget it would work like this:
fig = tools.make_subplots(rows=2, cols=2, shared_yaxes=True, subplot_titles=('Plot 1', 'Plot 2',
'Plot 3', 'Plot 4'))
But fig.update(shared_yaxes=True)
or fig.update(subplot_titles=('Plot 1', 'Plot 2', 'Plot 3', 'Plot 4')) won't work.
Hi @LePuppy ,
The graph_objs classes are designed to follow the Plotly.js plot schema as closely as possible. So we won't be able to introduce extra properties there. The general approach that plotly.py takes is to use make_subplots (and the figure_factories for that matter) to construct more sophisticated combinations of layout and trace properties, but the results of these need to follow that standard Plotly.js schema.
Thanks!
@jonmmease I noticed I did the wrong thing. I just had to declare arguments inside the make_subplots function like this:
fig = go.FigureWidget(make_subplots(rows=2, cols=2, shared_yaxes=True, subplot_titles=('Subplot 1',
'Subplot 2',
'Subplot 3',
'Subplot 4')))