【发布时间】:2021-12-09 12:22:33
【问题描述】:
我有一些按天细分的数据。对于每一天,我在一天的开始和结束时都有一个数据点,每个数据点的值都在 0 到 100 之间。我需要将此数据显示为分组条形图,其中 x 轴上的日期,y 轴上的值条形颜色由它们的值决定。对于每一天,左边栏需要有相应的开始值,右边栏显示当天的结束值。然而,图例需要显示颜色信息而不是迹线 情节基本上需要看起来像这样,但图例需要显示“绿色”、“琥珀色”、“红色”而不是“开始”、“结束”。
I need the plot to look like this but with a legend describing the colors rather than the traces
这里是一些重现情节的代码:
x = ["day"+str(i) for i in range(1,8)]
starts = [10, 50, 70, 75, 20, 50, 90]
ends = [95, 5, 80, 20, 50, 10, 75]
starts_colors = ['green', 'orange', 'red', 'red', 'green', 'orange', 'red']
ends_colors = ['red', 'green', 'red', 'green', 'orange', 'green', 'red']
这是我上面的情节的代码。
layout = go.Layout(showlegend=True)
fig = go.Figure(layout=layout)
fig.add_trace(go.Bar(x=x, y=starts, name = 'start', marker=dict(color=starts_colors)))
fig.add_trace(go.Bar(x=x, y=ends, name = 'end', marker=dict(color=ends_colors)))
fig.show()
如果我将数据重新排列成 3 条轨迹(每种颜色一条),并在开始和结束时使用相应的值,我最终会在条形之间出现间隙。例如,“day1”会在中间有一个间隙,因为“day1”没有橙色条。
这似乎是一个简单的问题,但我不知道如何让它按照我应该的方式工作。
【问题讨论】:
标签: legend plotly-python