ben
ben

Reputation: 43

I can't understand why "ax=ax" meaning in matplotlib

from datetime import datetime
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
data=pd.read_csv(r"C:\Users\champion\Desktop\ch02\spx.csv")
spx=data["SPX"]
spx.plot(**ax=ax**,style="k-")

I can't understand why "ax=ax" meaning in matplotlib.

Upvotes: 2

Views: 15363

Answers (3)

Felipe Pugian
Felipe Pugian

Reputation: 11

The current thought of the previous explanations are true, but it's an argument for a Series.plot() method.

Importing this

data=pd.read_csv(r"C:\Users\champion\Desktop\ch02\spx.csv")

What you are getting is a DataFrame.

And then:

spx=data["SPX"]

The code above is giving you a Series back. So, we are dealing with a Series.plot() method - but there is the same argument dor DataFrame.plot().

First of all, it is important to understand that this plot() method is correlated but not the same as the plot() function from matplotlib.

When you create a figure with:

fig=plt.figure()

It creates something like a blank sheet, and you can't create a plotting in a blank sheet.

The next code creates a subplot where you can finally plot something.

ax=fig.add_subplot(1,1,1)

And now we are getting to the question.

spx.plot(ax=ax,style="k-")

This piece of code is calling the plot method for a Series, and inside this method there is an optional argument called 'ax'.

The description of this argument says that it is an object of plotting from matplotlib for this plotting you want to do. If nothing is specified in there, so it makes use of the active subplotting of matplotlib.

Long story short, in your example there is only one active subplotting, and it is the 'ax' that was created before, so you could run your code without 'ax=ax', with the same result.

But it will make sense in a context when you have more than one subplotting object, so you could specify in wich one you would like to plot the spx Series.

We could have created a second and a third subplot, like this:

ax1 = fig.add_subplot(2, 2, 2)
ax2 = fig.add_subplot(2, 2, 3)

In this case, if I want to plot that Series on the 'ax1', I could have passed that to the argument:

spx.plot(ax=ax1,style="k-")

And now it is plotting on the exact box I wanted in the figure.

Upvotes: 1

John James
John James

Reputation: 11

Ax is the keyword for the part of the overall figure in which a chart/plot is drawn. So, when you type "spx.plot(**ax=", you are declaring the values for that part of the figure. The reason you are saying "ax=ax" is, as Nahal rightly pointed out, because you defined a variable named "ax" on the third line of code and you are using that to say what the ax keyword should be.

Here's an article with some helpful visuals.

https://towardsdatascience.com/what-are-the-plt-and-ax-in-matplotlib-exactly-d2cf4bf164a9

Upvotes: 1

Nehal J Wani
Nehal J Wani

Reputation: 16639

From the documentation of plot():

DataFrame.plot(x=None, y=None, kind='line', ax=None, subplots=False, sharex=None, sharey=False, layout=None, figsize=None, use_index=True, title=None, grid=None, legend=True, style=None, logx=False, logy=False, loglog=False, xticks=None, yticks=None, xlim=None, ylim=None, rot=None, fontsize=None, colormap=None, table=False, yerr=None, xerr=None, secondary_y=False, sort_columns=False, **kwds)

Parameters: ax : matplotlib axes object, default None

You can see that ax is a keyword argument here. It just happens that you also named your variable as ax and you are sending it as the value of that keyword argument to the function plot().

Upvotes: 4

Related Questions