4

我正在尝试通过情节创建四个笛卡尔象限。我无法将刻度移动到 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 中编辑,并以红色边框突出显示)

在此处输入图像描述

4

1 回答 1

3

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

在此处输入图像描述

于 2020-08-27T00:21:18.417 回答