【问题标题】:Bland-Altman plot move x axis to y=0 [duplicate]Bland-Altman 绘图将 x 轴移动到 y = 0 [重复]
【发布时间】:2018-03-27 10:25:03
【问题描述】:

对不起,如果这是一个重复的问题,我找不到任何对我有用的代码(对此仍然很新!)

我想移动 x 轴与 y=0 对齐,谢谢!

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

values = pd.read_csv(r'C:\Project\Mech_Data.csv', index_col=0)
g = sns.lmplot(x="mean", y="difference", hue="Group", markers=["o", "x"], data=values, palette=colors, fit_reg=False, legend=False)
plt.xlim(xmin=0)

plt.plot([0, 1400],[127.5461,127.5461], linewidth=2, color='#f1e4d1',linestyle=':') #mean
plt.plot([0, 1400],[-217.3879288,-217.3879288], linewidth=2, color='#f1e4d1',linestyle='-.') #-SD
plt.plot([0, 1400],[472.4801288,472.4801288], linewidth=2, color='#f1e4d1',linestyle='-.') #+SD

leg = plt.legend(loc='upper left')
leg.get_frame().set_edgecolor('b')

plt.title('Bland-Altman', loc='left', fontsize=12, fontweight=0, color='#512443')
plt.xlabel('Mean')
plt.ylabel('Difference')
plt.tight_layout()

plt.show()

Output plot

【问题讨论】:

  • 是和不是。 OP 不是要求居中的 x 轴,而是要求 y=0 处的 x 轴。不过语法非常相似。
  • 嗯,是的,应该是.set_position('zero') - matplotlib.org/examples/pylab_examples/…
  • 嗨!抱歉,我知道您可以添加一条直线作为附加轴,但我只想将上图中的 x 轴移动到绘图的中间(y=0 处)标签等等!
  • 感谢您的回复 - 我在哪里添加 .set_position('zero') ?

标签: python matplotlib


【解决方案1】:

您需要在plt.show() 调用之前添加以下行。

# get the axis 
ax = plt.gca()

# enforce placement of x-axis at y=0
# option 1)
ax.spines['bottom'].set_position('zero')

# option 2)
ax.spines['bottom'].set_position(('data', 0.))

【讨论】:

  • 太棒了,谢谢!
  • 这个问题问得很好。对于那些:任何时候。 ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多