I encounter an issue with plotly. I would like to display different figures but, somehow, I can't manage to achieve what I want.
I created 2 sources of data:
from plotly.graph_objs.scatter import Line
import plotly.graph_objs as go
trace11 = go.Scatter(
x = [0, 1, 2],
y = [0, 0, 0],
line = Line({'color': 'rgb(0, 0, 128)', 'width': 1})
)
trace12 = go.Scatter(
x=[0, 1, 2],
y=[1, 1, 1],
line = Line({'color': 'rgb(128, 0, 0)', 'width': 1})
)
trace21 = go.Scatter(
x = [0, 1, 2],
y = [0.5, 0.5, 0.5],
line = Line({'color': 'rgb(0, 0, 128)', 'width': 1})
)
trace22 = go.Scatter(
x=[0, 1, 2],
y=[1.5, 1.5, 1.5],
line = Line({'color': 'rgb(128, 0, 0)', 'width': 1})
)
data1 = [trace11, trace12]
data2 = [trace21, trace22]
Then, I created a subplot with 1 row and 2 columns and tried to add this data to the subplot:
from plotly import tools
fig = tools.make_subplots(rows=1, cols=2)
fig.append_trace(data1, 1, 1)
fig.append_trace(data2, 1, 2)
fig.show()
That resulted in the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-ba20e4900d41> in <module>
1 from plotly import tools
2 fig = tools.make_subplots(rows=1, cols=2)
----> 3 fig.append_trace(data1, 1, 1)
4 fig.append_trace(data2, 1, 2)
5 fig.show()
~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in append_trace(self, trace, row, col)
1797 )
1798
-> 1799 self.add_trace(trace=trace, row=row, col=col)
1800
1801 def _set_trace_grid_position(self, trace, row, col, secondary_y=False):
~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in add_trace(self, trace, row, col, secondary_y)
1621 rows=[row] if row is not None else None,
1622 cols=[col] if col is not None else None,
-> 1623 secondary_ys=[secondary_y] if secondary_y is not None else None,
1624 )
1625
~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in add_traces(self, data, rows, cols, secondary_ys)
1684
1685 # Validate traces
-> 1686 data = self._data_validator.validate_coerce(data)
1687
1688 # Set trace indexes
~\Anaconda3\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v, skip_invalid)
2667
2668 if invalid_els:
-> 2669 self.raise_invalid_elements(invalid_els)
2670
2671 v = to_scalar_or_list(res)
~\Anaconda3\lib\site-packages\_plotly_utils\basevalidators.py in raise_invalid_elements(self, invalid_els)
296 pname=self.parent_name,
297 invalid=invalid_els[:10],
--> 298 valid_clr_desc=self.description(),
299 )
300 )
ValueError:
Invalid element(s) received for the 'data' property of
Invalid elements include: [[Scatter({
'line': {'color': 'rgb(0, 0, 128)', 'width': 1}, 'x': [0, 1, 2], 'y': [0, 0, 0]
}), Scatter({
'line': {'color': 'rgb(128, 0, 0)', 'width': 1}, 'x': [0, 1, 2], 'y': [1, 1, 1]
})]]
The 'data' property is a tuple of trace instances
that may be specified as:
- A list or tuple of trace instances
(e.g. [Scatter(...), Bar(...)])
- A single trace instance
(e.g. Scatter(...), Bar(...), etc.)
- A list or tuple of dicts of string/value properties where:
- The 'type' property specifies the trace type
One of: ['area', 'bar', 'barpolar', 'box',
'candlestick', 'carpet', 'choropleth',
'choroplethmapbox', 'cone', 'contour',
'contourcarpet', 'densitymapbox', 'funnel',
'funnelarea', 'heatmap', 'heatmapgl',
'histogram', 'histogram2d',
'histogram2dcontour', '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']
- All remaining properties are passed to the constructor of
the specified trace type
(e.g. [{'type': 'scatter', ...}, {'type': 'bar, ...}])
I mist be doing something wrong. What is weird is that my data seems correctly shaped since I can run the following code without any issue:
fig = go.Figure(data1)
fig.show()
I hope you can help me find a solution.
Thanks!
The reason why you are getting an error it is because the function append_trace()
is expecting a single trace in the form you've declared them. However, the graph object Figure has the function add_traces()
with which you can pass the data parameter as a list with more than one trace.
Therefore, I suggest two simple solutions:
Solution 1: Append traces individually
from plotly.graph_objs.scatter import Line
import plotly.graph_objs as go
trace11 = go.Scatter(x = [0, 1, 2], y = [0, 0, 0], line = Line({'color': 'rgb(0, 0, 128)', 'width': 1}))
trace12 = go.Scatter(x = [0, 1, 2], y = [1, 1, 1], line = Line({'color': 'rgb(128, 0, 0)', 'width': 1}))
trace21 = go.Scatter(x = [0, 1, 2], y = [0.5, 0.5, 0.5], line = Line({'color': 'rgb(0, 0, 128)', 'width': 1}))
trace22 = go.Scatter(x = [0, 1, 2], y = [1.5, 1.5, 1.5], line = Line({'color': 'rgb(128, 0, 0)', 'width': 1}))
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=2)
fig.append_trace(trace11, row=1, col=1)
fig.append_trace(trace12, row=1, col=1)
fig.append_trace(trace21, row=1, col=2)
fig.append_trace(trace22, row=1, col=2)
fig.show()
Solution 2: Use the function add_traces(data,rows,cols)
instead
from plotly.graph_objs.scatter import Line
import plotly.graph_objs as go
trace11 = go.Scatter(x = [0, 1, 2], y = [0, 0, 0], line = Line({'color': 'rgb(0, 0, 128)', 'width': 1}))
trace12 = go.Scatter(x = [0, 1, 2], y = [1, 1, 1], line = Line({'color': 'rgb(128, 0, 0)', 'width': 1}))
trace21 = go.Scatter(x = [0, 1, 2], y = [0.5, 0.5, 0.5], line = Line({'color': 'rgb(0, 0, 128)', 'width': 1}))
trace22 = go.Scatter(x = [0, 1, 2], y = [1.5, 1.5, 1.5], line = Line({'color': 'rgb(128, 0, 0)', 'width': 1}))
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=2)
data1 = [trace11, trace12]
data2 = [trace21, trace22]
fig.add_traces(data1, rows=1, cols=1)
fig.add_traces(data2, rows=1, cols=2)
fig.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With