0

Sorry if this is a repeat question, I couldn't find any code that worked for me (still quite new to this!)

I want to move the x axis to align with y=0, thank you!

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

Lex
  • 27
  • 4
  • Yes and no. OP is not asking for a centered x-axis, but for an x-axis at y=0. The syntax is very similar though. – Paul Brodersen Mar 27 '18 at 10:31
  • Well, yeah it would be `.set_position('zero')` - https://matplotlib.org/examples/pylab_examples/spine_placement_demo.html – DavidG Mar 27 '18 at 10:33
  • Hi! Sorry I know you can add a straight line as an additional axis, but I just want the x axis in the image above moved to the middle of the plot (at y=0) labels and all! – Lex Mar 27 '18 at 10:33
  • Thank you for your replies - where do I add the .set_position('zero') ? – Lex Mar 27 '18 at 10:34

1 Answers1

0

You will want to add the following lines before your plt.show() call.

# 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.))
Paul Brodersen
  • 11,221
  • 21
  • 38