I would like to display the graph overlappingly on the plotly.

Asked 2 years ago, Updated 2 years ago, 138 views

I'd like to display the graph overlappingly in Plotly, but I can't display it well.
Current Code (Python)

import numpy as np
import seaborn as sns

import plotly.graph_objs as go 
import plotly.offline aspy
import plotly

plotly.offline.init_notebook_mode(connected=False)

n_legends=12
x = np.range(0,1,01)
y=np.random.rand(n_legends,100)+\
  np.range(n_legends).reshape(-1,1)

def get_colorpalette(colorpalette,n_color):
    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 = 'Legendary {: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))

config=dict(data=[data0,data1],layout=layout)

py.iplot(fig)

Error


colors=get_colorpalette('hls',n_legends)
data0 = [
    go.Scatter(
        x = x, y = y[i], name = 'Legendary {: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))

config=dict(data=[data0,data1],layout=layout)

py.iplot(fig)
colors=get_colorpalette('hls',n_legends)
data0 = [
    go.Scatter(
        x = x, y = y[i], name = 'Legendary {: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))
​
config=dict(data=[data0,data1],layout=layout)
​
py.iplot(fig)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-e7ad04250d7b>in<module>
     17fig=dict(data=[data0,data1],layout=layout)
     18 
--- > 19py.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, configuration, aim_play)
    382 
    383# Get figure
-->384figure=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, validate_figure)
    551 
    552try:
-->553figure=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 default_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 invalidate_coerce(self,v,skip_invalid,_validate)
   2671 
   2672 if invalid_els:
->2673self.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 raise_invalid_elements(self, invalid_els)
    289 default_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.039999999999)'},
    'mode': 'lines',
    'name': 'Legendary 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

What should I do?
Sorry for the inconvenience, but I appreciate your cooperation.

python plotly-python

2022-09-30 17:55

1 Answers

Invalid element(s) received for the 'data' property..., so
Perhaps you should flatten the data=[data0,data1] part of fig=dict(data=[data0,data1],layout=layout).data=data0+data1

This post was posted as a community wiki based on @user39889's comments.


2022-09-30 17:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.