0

the example code from the website:

import plotly.graph_objects as go
from plotly.colors import n_colors
import numpy as np
np.random.seed(1)

# 12 sets of normal distributed random data, with increasing mean and standard deviation
data = (np.linspace(1, 2, 12)[:, np.newaxis] *random.randn(12, 200) + 
    (np.arange(12) + 2 * np.random.random(12))[:, np.newaxis])

colors = n_colors('rgb(5, 200, 200)', 'rgb(200, 10, 10)', 12, colortype='rgb')

fig = go.Figure()
for data_line, color in zip(data, colors):
    fig.add_trace(go.Violin(x=data_line, line_color=color))

fig.update_traces(orientation='h', side='positive', width=3, points=False)
fig.update_layout(xaxis_showgrid=False, xaxis_zeroline=False)
fig.show()

enter image description here

If I add name = 'Sample' to:

fig.add_trace(go.Violin(x=data_line, line_color=color, name = 'Sample'))

the chart changes dramatically and is meaningless. How Do I add a name to the traces so they no longer read 'trace 0', 'trace 1' etc. ?

enter image description here

Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
Jim R
  • 1
  • 2

2 Answers2

1

You can use the name argument of go.Violin. But the names should be different. Here is the modified example (I set it to the number of the trace just as an example):

import plotly.graph_objects as go
from plotly.colors import n_colors
import numpy as np
np.random.seed(1)

# 12 sets of normal distributed random data, with increasing mean and standard deviation
data = (np.linspace(1, 2, 12)[:, np.newaxis] *np.random.randn(12, 200) + 
    (np.arange(12) + 2 * np.random.random(12))[:, np.newaxis])

colors = n_colors('rgb(5, 200, 200)', 'rgb(200, 10, 10)', 12, colortype='rgb')

fig = go.Figure()
for i, e in enumerate(zip(data, colors)):
    data_line, color = e
    fig.add_trace(go.Violin(x=data_line, line_color=color, name=i))

fig.update_traces(orientation='h', side='positive', width=3, points=False)
fig.update_layout(xaxis_showgrid=False, xaxis_zeroline=False)
fig.show()

enter image description here

Stanislas Morbieu
  • 1,721
  • 7
  • 11
  • this is great. however when I extend it to fit my needs, all the graphs flatten out. I have a list called Dates: Dates = ['2001-06','2005-06','2009-06', '2014-06', '2019-06'] and I make the small change name=Dates[i] in my code the graphs change – Jim R Nov 19 '19 at 03:10
  • my graph doesn't have as many lines - ie why the Dates list is shorter – Jim R Nov 19 '19 at 03:21
  • if I substitute name=Dates[i], and Dates = ['2001-06','2005-06','2009-06', '2010-06','2011-06','2012-06', '2013-06','2014-06','2015-06','2016-06','2017-06','2019-06'] , graphs turn flat – Jim R Nov 19 '19 at 03:32
  • It seems Plotly parses the dates for the y axis and therefore we have flat lines. If you add something to the date string, for instance a dot at the end (``name=f"{Dates[i]}."``), you have the desired behavior. I don't know if there is a better solution. – Stanislas Morbieu Nov 19 '19 at 09:12
0

Don't let plotly convert the datetime implicitly. Use strftime() to get your desired format and use that value to update the "name" or "y0" attribute to get the desired results. This will resolve the issue, however I think it's a bug in Plotly. I have raised an issue. Until then use this hack :)

Follow the below example

fig.add_trace(go.Violin(x = violin_plot_data.loc[:,metric_name] , 
                                    orientation = 'h',
                                    width = 3 ,
                                    side = 'positive',
                                    name = date.strftime('%b %d'),
                                    meanline_visible=True,
                                    points = False,
                                   )
                         )

Output Image:

enter image description here

buddemat
  • 4,552
  • 14
  • 29
  • 49