dbs5
dbs5

Reputation: 133

Why can I create a plot using df.plot, but then modify the plot I see by calling the plt object?

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).

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()

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?

Upvotes: 0

Views: 151

Answers (1)

Mark Snyder
Mark Snyder

Reputation: 1655

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.

Upvotes: 2

Related Questions