如何使用matplotlib绘制这样的图形?
答案 0 :(得分:1)
例如:
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()
或者更接近你的照片(这里我删除了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()