从以下内容来看:
我正在研究fig.update_xaxes函数,但只能更改当前网格中的白色线条颜色。
听起来你实际上是在问如何更改沿y轴特定区段的背景颜色, 特别是因为你还说到:
[...]以便条形图更加清晰可见。
如果确实是这种情况,那么我会使用具有交替背景颜色的合理间隔的shapes,并将shapes设置为出现在图表的traces之后以获得如下效果:
如果你想隐藏网格线,你可以添加 xaxis=dict(showgrid=False)
:
import plotly.graph_objects as go
import numpy as np
y = ['giraffes', 'orangutans', 'monkeys']
x = [20, 14, 23]
fig = go.Figure(go.Bar(
x=x,
y=y,
orientation='h'))
# find step size for an interval for the number of y-values
steps = 1/len(y)
# set up where each interval ends
ends = [0 + steps*(e+1) for e in np.arange(0, len(y))]
# container for shapes to be added as backgrounds
shapes = []
# super-easy way of making a list for alternating backgrounds
colors = ['grey', 'rgba(0,0,0,0)']*len(y)
# set up shapes for alternating background colors
for i, e in enumerate(ends):
shapes.append(dict(type="rect",
xref="paper",
yref="paper",
x0=0,
y0=e-steps,
x1=1,
y1=e,
fillcolor=colors[i],
opacity=0.5,
layer="below",
line_width=0,
)
)
# fig.update_layout(xaxis=dict(showgrid=True), shapes=shapes)
# fig.show()
fig.update_layout(xaxis=dict(showgrid=False), shapes=shapes)
fig.show()