Diego Rueda
Diego Rueda

Reputation: 2506

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?

Upvotes: 4

Views: 20195

Answers (2)

FOR Vpbof-20
FOR Vpbof-20

Reputation: 41

Try

ax.spines['left'].set_position(('data', 0))

Upvotes: 4

kaminsknator
kaminsknator

Reputation: 1183

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

Upvotes: 4

Related Questions