plotlyで、グラフを重ねて表示させたいのですが、うまく表示できません。
現在のコード(Python)
import numpy as np
import seaborn as sns
import plotly.graph_objs as go
import plotly.offline as py
import plotly
plotly.offline.init_notebook_mode(connected=False)
n_legends = 12
x = np.arange(0, 1, .01)
y = np.random.rand(n_legends, 100) + \
np.arange(n_legends).reshape(-1, 1)
def get_colorpalette(colorpalette, n_colors):
palette = sns.color_palette(
colorpalette, n_colors)
rgb = ['rgb({},{},{})'.format(*[x*256 for x in rgb])
for rgb in palette]
return rgb
colors = get_colorpalette('hls', n_legends)
data0 = [
go.Scatter(
x=x, y=y[i], name='凡例 {:02d}'.format(i),
marker={'color':colors[i]}, mode='lines')
for i in range(n_legends)]
data1 = [
go.Scatter(
x=x, y=y[i], name=' {:02d}'.format(i),
marker={'color':colors[i]}, mode='lines+markers')
for i in range(n_legends)]
layout = go.Layout(
xaxis=dict(title=dict(text="x_axis", font=dict(size=16)), tickfont=dict(size=16), autorange=True))
fig = dict(data=[data0, data1], layout=layout)
py.iplot(fig)
エラー
colors = get_colorpalette('hls', n_legends)
data0 = [
go.Scatter(
x=x, y=y[i], name='凡例 {:02d}'.format(i),
marker={'color':colors[i]}, mode='lines')
for i in range(n_legends)]
data1 = [
go.Scatter(
x=x, y=y[i], name=' {:02d}'.format(i),
marker={'color':colors[i]}, mode='lines+markers')
for i in range(n_legends)]
layout = go.Layout(
xaxis=dict(title=dict(text="x_axis", font=dict(size=16)), tickfont=dict(size=16), autorange=True))
fig = dict(data=[data0, data1], layout=layout)
py.iplot(fig)
colors = get_colorpalette('hls', n_legends)
data0 = [
go.Scatter(
x=x, y=y[i], name='凡例 {:02d}'.format(i),
marker={'color':colors[i]}, mode='lines')
for i in range(n_legends)]
data1 = [
go.Scatter(
x=x, y=y[i], name=' {:02d}'.format(i),
marker={'color':colors[i]}, mode='lines+markers')
for i in range(n_legends)]
layout = go.Layout(
xaxis=dict(title=dict(text="x_axis", font=dict(size=16)), tickfont=dict(size=16), autorange=True))
fig = dict(data=[data0, data1], layout=layout)
py.iplot(fig)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-e7ad04250d7b> in <module>
17 fig = dict(data=[data0, data1], layout=layout)
18
---> 19 py.iplot(fig)
c:\users\owner\appdata\local\programs\python\python38-32\lib\site-packages\plotly\offline\offline.py in iplot(figure_or_data, show_link, link_text, validate, image, filename, image_width, image_height, config, auto_play, animation_opts)
382
383 # Get figure
--> 384 figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
385
386 # Handle image request
c:\users\owner\appdata\local\programs\python\python38-32\lib\site-packages\plotly\tools.py in return_figure_from_figure_or_data(figure_or_data, validate_figure)
551
552 try:
--> 553 figure = Figure(**figure).to_dict()
554 except exceptions.PlotlyError as err:
555 raise exceptions.PlotlyError(
c:\users\owner\appdata\local\programs\python\python38-32\lib\site-packages\plotly\graph_objs\_figure.py in __init__(self, data, layout, frames, skip_invalid, **kwargs)
582 is invalid AND skip_invalid is False
583 """
--> 584 super(Figure, self).__init__(data, layout, frames, skip_invalid, **kwargs)
585
586 def add_area(
c:\users\owner\appdata\local\programs\python\python38-32\lib\site-packages\plotly\basedatatypes.py in __init__(self, data, layout_plotly, frames, skip_invalid, **kwargs)
144
145 # ### Import traces ###
--> 146 data = self._data_validator.validate_coerce(
147 data, skip_invalid=skip_invalid, _validate=self._validate
148 )
c:\users\owner\appdata\local\programs\python\python38-32\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v, skip_invalid, _validate)
2671
2672 if invalid_els:
-> 2673 self.raise_invalid_elements(invalid_els)
2674
2675 v = to_scalar_or_list(res)
c:\users\owner\appdata\local\programs\python\python38-32\lib\site-packages\_plotly_utils\basevalidators.py in raise_invalid_elements(self, invalid_els)
289 def raise_invalid_elements(self, invalid_els):
290 if invalid_els:
--> 291 raise ValueError(
292 """
293 Invalid element(s) received for the '{name}' property of {pname}
ValueError:
Invalid element(s) received for the 'data' property of
Invalid elements include: [[Scatter({
'marker': {'color': 'rgb(220.16,95.0272,87.03999999999999)'},
'mode': 'lines',
'name': '凡例 00',
'x': array([0. , 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 , 0.11,
0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 , 0.21, 0.22, 0.23,
0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 , 0.31, 0.32, 0.33, 0.34, 0.35,
0.36, 0.37, 0.38, 0.39, 0.4 , 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47
どのようにすれば良いのでしょうか?
お手数をおかけしますが、よろしくお願いいたします。
Invalid element(s) received for the 'data' property ...
と表示されているので、おそらくfig = dict(data=[data0, data1], layout=layout)
のdata=[data0, data1]
の部分を flatten すればよいかと思います。data=data0+data1
data=data0+data1
に変更することで、自分のやりたかったことが出来ました。ありがとうございます。