我试图理解为什么我的代码不能使用‘.’完成。运算符在Eclipse中,即使代码成功运行。守则如下:
import matplotlib.pyplot as plt
# I can use either of these and get the same result.
# ax = plt.subplot() #(1)
ax = plt.gca() #(2)
ax.plot(people, total_cost, color = 'red') #(3)
ax.set_xlabel('# People') #(4)
ax.set_ylabel('Cost\n(Parking)\t(ticket)') #(5)我不明白的是,虽然语句(4)和(5)相应地绘制了一个图,但‘ax’的属性根本不是来自‘pyplot.axes’类,而是来自‘matplotlib.axes’类。语句(3)是来自pyplot.axes的调用
那么问题就来了。
中的有效语句。
虽然‘ax’可以从‘pyplot.axes’类定义为(1)或(2),但是语句(3)来自‘matplotlib.axes’类。那么,如何成功地调用两个类,而没有由于(1)或(2)而声明一个类?
谢谢你的帮助。
matplotlib.pyplot.plot https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
matplotlib.pyplot.axes https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.axes.html
matplotlib.axes https://matplotlib.org/stable/api/axes_api.html
发布于 2021-03-25 10:01:09
matplotlib.pyplot.axes是一个函数,而不是类。它创建matplotlib.axes.Axes类的一个实例,将其添加到当前图形中,并使其成为当前轴。
matplotlib.axes是一个模块,而不是类。它包含matplotlib.axes.Axes类,这是在调用函数创建轴实例(如plt.subplots()、fig.add_axes()、plt.gca()等)时创建的。
因此,无论您使用哪个方法(1)或(2),您的ax都将是一个matplotlib.axes.Axes实例。
https://stackoverflow.com/questions/66792901
复制相似问题