Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move Y axis to another position in matplotlib

I have the following plot:

Normal Plot

I want to move the y axis to x = 0 with ticks and everything, how can I do it?

like image 241
Diego Rueda Avatar asked Oct 21 '25 08:10

Diego Rueda


1 Answers

Here you go

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
x = np.linspace(-np.pi, np.pi, 100)
y = 2*np.sin(x)

ax = fig.add_subplot(111)
ax.set_title('centered spines')
ax.plot(x, y)
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('bottom')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

more examples:http://matplotlib.org/examples/pylab_examples/spine_placement_demo.html

like image 70
kaminsknator Avatar answered Oct 22 '25 23:10

kaminsknator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!