将Y轴移动到matplotlib中的另一个位置

时间:2015-07-21 18:59:14

标签: python matplotlib

我有以下情节:

Normal Plot

我想通过刻度和所有内容将y轴移动到x = 0,我该怎么办?

2 个答案:

答案 0 :(得分:2)

你去吧

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')

更多示例:http://matplotlib.org/examples/pylab_examples/spine_placement_demo.html

答案 1 :(得分:0)

尝试

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