为什么我可以使用 df.plot 创建绘图,然后通过调用 plt 对象修改我看到的绘图? [英] Why can I create a plot using df.plot, but then modify the plot I see by calling the plt object?

查看:61
本文介绍了为什么我可以使用 df.plot 创建绘图,然后通过调用 plt 对象修改我看到的绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我只是在这里遗漏了一些非常简单的东西.但我知道在 Pandas 中,我可以通过简单地执行 dataframe.plot (信息在这里).

Maybe I'm just missing something really straightforward here. But I know in pandas I can use the matplotlib plotting feature by simply doing dataframe.plot (info here).

但是我怎么能通过使用 plt.titleplt.xlabelplt.ylabel 来修改那个精确的图,等等.?这对我来说没有意义.作为参考,我正在关注 本教程

But how is it that I can modify that EXACT plot by just using plt.title, plt.xlabel, plt.ylabel, etc.? it doesn't make sense to me. For reference, I'm following this tutorial

dataset.plot(x='MinTemp', y='MaxTemp', style='o')  
plt.title('MinTemp vs MaxTemp')  
plt.xlabel('MinTemp')  
plt.ylabel('MaxTemp')  
plt.show()

这是否与当我在数据帧上运行 .plot 时,我真的在创建一个 matplotlib.pyplot 对象有关?

Does it have something to do with the fact that when I'm running .plot on a dataframe, I'm really creating a matplotlib.pyplot object?

推荐答案

Matplotlib 具有当前轴的概念.本质上,这意味着每当您第一次执行需要 axes 对象的操作时,都会为您创建一个对象,并成为您未来所有操作都将应用到的默认对象,直到您更改当前轴到别的东西.在您的情况下,dataset.plot(x='MinTemp', y='MaxTemp', style='o') 创建一个 axes 对象并将其设置为当前轴.所有 plt.title()plt.xlabel()plt.ylabel() 都只是将它们的更改应用到当前轴.

Matplotlib has the concept of the current axes. Essentially what this means is that whenever you first do something that requires an axes object, one is created for you and becomes the default object that all of your future actions will be applied to until you change the current axes to something else. In your case, dataset.plot(x='MinTemp', y='MaxTemp', style='o') creates an axes object and sets it as the current axes. All of plt.title(), plt.xlabel(), and plt.ylabel() simply apply their changes to the current axes.

这篇关于为什么我可以使用 df.plot 创建绘图,然后通过调用 plt 对象修改我看到的绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆