将轴线添加到 matplotlib 图

Bil*_*ore 5 python matplotlib ipython jupyter

我正在使用“ipython jupyter notebook”。我的问题是:

如何将轴线添加到图中,即。y=0 和 x=0:

%matplotlib inline
from numpy import *
from matplotlib.pyplot import *
nil=seterr(divide='ignore', invalid='ignore')

t = arange(-2, 2, 0.1)
y1 = exp(t)
y2 = exp(-t)

subplot(121)
title('y=exp(t)')
ylabel('y')
xlabel('t')
grid()
plot(t, y1, '-')

subplot(122)
title('y=exp(-t)')
ylabel('y')
xlabel('t')
grid()
plot(t, y2, '-')
show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Car*_* Ng 9

实现这一点的最简单方法(不幸的是,没有花哨的箭头)是使用axvlineaxhline分别在 x=0 和 y=0 处画线:

t = arange(-2, 2, 0.1)
y2 = exp(-t)
axhline(0,color='red') # x = 0
axvline(0,color='red') # y = 0
grid()
plot(t, y2, '-')
show()
Run Code Online (Sandbox Code Playgroud)

例子