matplotlib与iPython笔记本和python fi不符

2024-05-05 18:19:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我对matplotlib有一个奇怪的问题,我似乎想不出来。当使用带有pylab标志的ipython笔记本时,ipython notebook --pylab inline我有一行代码,看起来像是用matplotlib生成一个colorbar:

im = ax.imshow(df, vmin=vmin, vmax=vmax)

代码工作正常,我得到了一个不错的色条。当我以python文件的形式运行这段代码时,我得到一个错误,NameError: name 'ax' is not defined。我知道ipython notebook --pylab inline会自动将一堆内容导入到笔记本中,但我不知道需要导入什么才能解决问题。print type(ax)给出:

<class 'matplotlib.axes.AxesSubplot'>

有谁能指出为什么我的代码在ipython中工作,而不是在一个普通的python文件中?提前谢谢。


Tags: 文件代码matplotlib标志ipythoninline笔记本ax
2条回答

我不太清楚你做了什么,因为aX在默认情况下并没有被定义为pylab的一部分。

通常,ax指的是axis对象。有几种方法可以得到:

matplotlib.pyplot.gca()            # gca = get current axis
matplotlib.pyplot.subplot(2,1,1)   # For creating multiple plots in one figure
fig.get_axes()[x]                  # Where fig is a Figure object

我也有同样的问题。 根据此条目: How to abbreviate xtick labels years to 2 digits in a matplotlib plot

尝试通过添加(在导致错误的行之前)来定义“ax”:

ax = plt.gca()

相关问题 更多 >