use*_*305 8 python axes matplotlib
我做以下导入:
import matplotlib.pyplot as plt
import matplotlib.axes as ax
import matplotlib
import pylab
Run Code Online (Sandbox Code Playgroud)
它正确执行
plt.plot(y1, 'b')
plt.plot(y2, 'r')
plt.grid()
plt.axhline(1, color='black', lw=2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
并显示图表.
但如果我插入
print("ylim=", ax.get_ylim())
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
AttributeError:'module'对象没有属性'get_ylim'
我试过更换斧头.与plt.,matplotlib等,我得到相同的错误.
打电话的正确方法是get_ylim什么?
Max*_*Noe 11
不要导入matplotlib.axes,在您的示例中,您需要的唯一导入是matplotlib.pyplot
get_ylim()是matplotlib.axes.Axes的一种方法class.如果您使用pyplot绘制内容,则始终会创建此类.它代表坐标系,并具有将某些内容绘制到其中并进行配置的所有方法.
在您的示例中,您没有名为ax的Axes,您将matplotlib.axes模块命名为ax.
要使用matplotlib当前使用的轴 plt.gca().get_ylim()
或者你可以这样做:
fig = plt.figure()
ax = fig.add_subplot(1,1,1) # 1 Row, 1 Column and the first axes in this grid
ax.plot(y1, 'b')
ax.plot(y2, 'r')
ax.grid()
ax.axhline(1, color='black', lw=2)
print("ylim:" ax.get_ylim())
plt.show()
Run Code Online (Sandbox Code Playgroud)
如果您只想使用pyplot API:plt.ylim()也返回ylim.