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

4

我正在试图通过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()

我得到了下面的图表: enter image description here 但是,我希望创建像下面这样的图表,即将刻度移动到x和y轴(在PowerPoint中进行编辑,并用红色边框突出显示): enter image description here
1个回答

4

我认为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()

enter image description here


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接