图表,横穿y轴

时间:2011-12-20 21:10:54

标签: python matplotlib

如何使用matplotlib绘制这样的图形?

graph, that crosses y axis

1 个答案:

答案 0 :(得分:1)

this example

查看this group

例如:

from matplotlib import pyplot as plt
import numpy as np

def line(x, slope=1, zero=0):
    return zero + slope * x

x = np.array([-4,10])
y1 = line(x, 2, 2)
y2 = line(x, 1, 3)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y1)
ax.plot(x,y2)

ax.spines['left'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position(('data',0))
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')

plt.show()

enter image description here

或者更接近你的照片(这里我删除了set_smart_bounds,因为在win7中似乎对示例没有效果):

ax.spines['left'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position(('data',0))
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.ylim(ymin=0)
plt.show()

enter image description here