如何在 Plotly 中设置非零零线

How to set non-zero zeroline in Plotly

我正在尝试使用温度在 Plotly 中制作条形图,但希望零线在华氏温度下以 32 为中心而不是 0(这在摄氏模式下很好)。这样,低于 32F 的值会使条形图下降而不是上升。

附上预期行为的示例(之前是在 HighCharts 而不是 Plotly 中完成的)

y0、dy 或 zeroline 似乎都不允许这种行为。

您可能正在寻找的参数具有直观的名称 base

base (number or categorical coordinate string)

Sets where the bar base is drawn (in position axis units). In "stack" or "relative" barmode, traces that set "base" will be excluded and drawn in "overlay" mode instead.

您可以在此处使用列表或仅使用单个值。

至少现在条形图移动了。接下来我们需要将 yaxiszeroline 设置为 False 来隐藏它,最后通过 shapes 添加我们自己的零线。

import plotly
plotly.offline.init_notebook_mode()

val_celcius = [-50, 0, 50, 100]
val_fahrenheit = [c * 1.8 for c in val_celcius] # we don't need +32 here because of the shift by `base`

x = [i for i, _ in enumerate(val_celcius)]

data = plotly.graph_objs.Bar(x=[0, 1, 2, 3], 
                             y=val_fahrenheit,
                             text=['{}°C'.format(c) for c in val_celcius],
                             base=32)
layout = plotly.graph_objs.Layout(yaxis={'zeroline': False},
                                  shapes=[{'type': 'line', 
                                           'x0': 0, 'x1': 1, 'xref': 'paper',
                                           'y0': 32, 'y1': 32, 'yref': 'y'}])
fig = plotly.graph_objs.Figure(data=[data], layout=layout)
plotly.offline.iplot(fig)