【问题标题】:Plotly Violin Plot Add Trace cannot add namesPlotly Violin Plot Add Trace 无法添加名称
【发布时间】:2023-03-09 10:23:02
【问题描述】:

来自网站的示例代码:

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()

如果我将 name = 'Sample' 添加到:

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

图表变化很大,毫无意义。如何为跟踪添加名称,使其不再读取“跟踪 0”、“跟踪 1”等?

【问题讨论】:

    标签: python plotly violin-plot


    【解决方案1】:

    您可以使用go.Violinname 参数。 但名称应该不同。下面是修改后的例子(我把它设置为trace的编号作为例子):

    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()
    

    【讨论】:

    • 这很棒。但是,当我扩展它以满足我的需要时,所有图表都变平了。我有一个名为 Dates 的列表: Dates = ['2001-06','2005-06','2009-06', '2014-06', '2019-06'] 我做了一个小的更改 name=Dates[ i] 在我的代码中,图表会发生变化
    • 我的图表没有那么多行 - 即为什么日期列表更短
    • 如果我替换 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'] ,图表变平跨度>
    • 似乎 Plotly 解析了 y 轴的日期,因此我们有平线。如果您在日期字符串中添加一些内容,例如末尾的点 (name=f"{Dates[i]}."),您将获得所需的行为。不知道有没有更好的解决办法。
    【解决方案2】:

    不要让隐式转换日期时间。使用strftime() 获取所需的格式并使用该值更新“名称”或“y0”属性以获得所需的结果。这将解决问题,但我认为这是 Plotly 中的一个错误。我提出了一个问题。在此之前使用这个 hack :)

    按照以下示例进行操作

    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:

    【讨论】:

    • 考虑添加指向您提出的问题的链接。
    猜你喜欢
    • 2021-12-23
    • 2022-07-14
    • 2021-10-13
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 2020-02-13
    • 2021-09-15
    • 2021-06-25
    相关资源
    最近更新 更多