在 Plotly Python 中移动 x 轴和 y 轴刻度

Posted

技术标签:

【中文标题】在 Plotly Python 中移动 x 轴和 y 轴刻度【英文标题】:Moving the x-axis and y-axis ticks in Plotly Python 【发布时间】:2020-12-15 16:10:12 【问题描述】:

我正在尝试通过 plotly 创建四个笛卡尔象限。我无法将刻度移动到 x=0 和 y=0。我无法弄清楚如何。

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[-4,-4, -4 ,-4, -4],fill='tozeroy', name = 'Cost Saving', mode = 'lines',line=dict(width=0.5, color='rgb(144,238,144,0.2)'), opacity = 0.2)) 
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[4, 4, 4, 4, 4], fill='tozeroy', name = 'Highly Cost Effective', mode = 'lines',line=dict(width=0.5, color='rgb(173,216,230,0.1)'), opacity = 0.2)) # fill down to xaxis
fig.add_trace(go.Scatter(x=[0, -1, -2, -3, -4], y=[-4,-4, -4 ,-4, -4],fill='tozeroy', name = 'Dominated', mode = 'lines',line=dict(width=0.5, color='rgb(211,211,211,0.05)'), opacity = 0.2)) # fill to trace0 y

daly = [ 2,3.3, 1, 0.7, 2.3]
cost = [ 1,-0.3, 2, -0.7, 3.3]
name = ['A', 'B', 'C', 'D', 'E']

fig.add_trace(go.Scatter(x=daly, y = cost, mode='markers+text', text = name,textposition='top right', marker=dict(size=10, color='black'), showlegend= False))

fig.update_layout(yaxis_range=(-2, 4), xaxis_range=(-1,4))
fig.update_xaxes(zeroline=True, zerolinewidth=2, zerolinecolor='Black')
fig.update_yaxes(zeroline=True, zerolinewidth=2, zerolinecolor='Black')

fig.show()

我得到下面的情节:

但是,我希望创建类似下面的内容,即移动到 x,y 的刻度(在 powerpoint 中编辑,并以红色边框突出显示)

【问题讨论】:

【参考方案1】:

我认为 Plotly 不允许您将坐标轴的刻度线移动到绘图内部。一种解决方法是禁用刻度线,然后通过为 x 轴和 y 轴创建整数范围并在绘图上显示这些文本来添加您自己的注释。您可以尝试在哪里绘制这些注释以及如何将它们从轴上偏移。

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[-4,-4, -4 ,-4, -4],fill='tozeroy', name = 'Cost Saving', mode = 'lines',line=dict(width=0.5, color='rgb(144,238,144,0.2)'), opacity = 0.2)) 
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[4, 4, 4, 4, 4], fill='tozeroy', name = 'Highly Cost Effective', mode = 'lines',line=dict(width=0.5, color='rgb(173,216,230,0.1)'), opacity = 0.2)) # fill down to xaxis
fig.add_trace(go.Scatter(x=[0, -1, -2, -3, -4], y=[-4,-4, -4 ,-4, -4],fill='tozeroy', name = 'Dominated', mode = 'lines',line=dict(width=0.5, color='rgb(211,211,211,0.05)'), opacity = 0.2)) # fill to trace0 y

daly = [ 2,3.3, 1, 0.7, 2.3]
cost = [ 1,-0.3, 2, -0.7, 3.3]
name = ['A', 'B', 'C', 'D', 'E']

fig.add_trace(go.Scatter(x=daly, y = cost, mode='markers+text', text = name, textposition='top right', marker=dict(size=10, color='black'), showlegend= False))

fig.update_layout(yaxis_range=(-2, 4), xaxis_range=(-1,4), xaxis=dict(showticklabels=False), yaxis=dict(showticklabels=False))
fig.update_xaxes(zeroline=True, zerolinewidth=2, zerolinecolor='Black')
fig.update_yaxes(zeroline=True, zerolinewidth=2, zerolinecolor='Black')

# generate a list of the integer values for tick marks
x_values = list(range(-2,5))
y_values = list(range(-2,5))

# add the list of values as annotations, offsetting the coordinates using the parameters x,y
xaxes_dict = [dict(x=x_val+0.05, y=0.1, xref="x", yref="y", text=str(x_val), showarrow=False) for x_val in x_values]
yaxes_dict = [dict(x=-0.02, y=y_val-0.1, xref="x", yref="y", text=str(y_val), showarrow=False) for y_val in y_values]

xaxis_title = [dict(x=2.6, y=-0.1, xref="x", yref="y", text="your xtitle", showarrow=False)]
yaxis_title = [dict(x=-0.1, y=2.0, xref="x", yref="y", text="your ytitle", textangle=-90, showarrow=False)]
axes_dict = xaxes_dict + yaxes_dict + xaxis_title + yaxis_title

fig.update_layout(annotations=axes_dict)
fig.show()

【讨论】:

非常感谢。这对我有用。使用类似的解决方法,轴标题是否也可以沿相同的 x,y = 0 显示? 是的,当然。您可以添加更多注释,并再次使用xy 参数指定标题的位置。我已经用一些示例轴标题更新了我的答案

以上是关于在 Plotly Python 中移动 x 轴和 y 轴刻度的主要内容,如果未能解决你的问题,请参考以下文章

在 plotly express 中,如何重命名轴和图例的名称? y 轴仅显示为值,图例上的标题是可变的

Plotly 可移动/可拖动图例,python

在 F# 中使用 xplot/plotly 的连续误差带

如何在plotly python中将箱形图中位数与多类别x轴连接起来

python使用matplotlib可视化为可视化图像的X轴和Y轴设置自定义的轴标签(axis labels of matplotlib plot)

如何在 python 中使用 plotly 绘制累积数据?