gpt4 book ai didi

python - 导入 pyplot 模块并调用 pyplot,plot() 但没有存储对任何变量的返回。但是调用 pyplot.show() 函数?

转载 作者:行者123 更新时间:2023-12-01 07:24:05 26 4
gpt4 key购买 nike

从 matplotlib 库中我导入了 pyplot 模块。在该模块中有一个我使用过的函数plot()。现在我的问题是:

  1. 为什么plot()函数不在任何类中?如果它在任何类中,为什么我们不创建该类的任何对象并使用plot()函数。

  2. 从官方文档中我了解到plot()返回一个Line2D对象。但是plot()的返回结果没有存储在任何变量中,但我们正在使用show()函数。一般来说,它应该是 returned_object.show() ,但 pyplot 模块有一个 show() 函数,调用该函数时不使用任何对象,就像plot() 函数一样。我们仅使用 pyplot.show() 来显示图表。这怎么可能。我的意思是它在 returned_object.show() 或 pyplot.show(从plot()函数返回的对象)中更有意义。它如何显示特定图而不是其他随机图?

我已经完成了示例代码并成功绘制。为了消除我的疑虑,我访问了官方的 matplotlib 模块。


from matplotlib import pyplot as plt

years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]

gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]

# create a line chart, years on x-axis, gdp on y-axis
plt.plot(years, gdp, color='green', marker='o', linestyle='solid')

# add a title
plt.title("Nominal GDP")

# add a label to the y-axis
plt.ylabel("Billions of $")

plt.show()

最佳答案

The docs说 pyplot is a state-based interface to matplotlib. 。它将绘图命令的当前状态保留在其内部 - 例如当前的轴、图形、艺术家...

来自Tutorial :

In matplotlib.pyplot various states are preserved across function calls, so that it keeps track of things like the current figure and plotting area, and the plotting functions are directed to the current axes (please note that "axes" here and in most places in the documentation refers to the axes part of a figure and not the strict mathematical term for more than one axis).

<小时/>

这些函数还返回艺术家、线条等,因此您可以将它们分配给名称并将其属性修改为您心中的内容。

<小时/>

值得一读the Tutorials至少通过传奇指南

关于python - 导入 pyplot 模块并调用 pyplot,plot() 但没有存储对任何变量的返回。但是调用 pyplot.show() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57541495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com