David
David

Reputation: 95

Matplotlib.axes v matplotlib.pyplot.axes in Eclipse

I am trying to understand why my code will not complete using the ‘.’ operator within the Eclipse IDE, even although the code runs successfully. The code is as follows:

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)

What I do not understand is, while the statements (4) and (5) draw a graph accordingly, the attributes of ‘ax’ are not from the ‘pyplot.axes’ class at all, but from the ‘matplotlib.axes’ class. Statement (3) is a call from pyplot.axes

The questions would be then.

  1. Why can’t I code complete the code using the ‘.’ operator, even though each is a valid statement in (3), (4) and (5) ?

2 While ‘ax’ can be defined as either (1) or (2) from the ‘pyplot.axes’ class, statement (3) is from the ‘matplotlib.axes’ class. How then are both classes being called successfully without one being declared as a result of (1) or (2) ?

  1. Just as a comment, the arguments for ’matplotlib.axes’ and ‘matplotlib.pyplot.axes’ appear to be the same.

Thanks for any help.

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

Upvotes: 0

Views: 137

Answers (1)

tmdavison
tmdavison

Reputation: 69193

matplotlib.pyplot.axes is a function, not a class. It creates an instance of the matplotlib.axes.Axes class, adds it to the current figure, and makes it the current axes.

matplotlib.axes is a module, not a class. It contains the matplotlib.axes.Axes class, which is what gets created when you call a function to create an Axes instance, such as plt.subplots(), fig.add_axes(), plt.gca(), etc.

So, whichever of your methods (1) or (2) you use, your ax will be a matplotlib.axes.Axes instance.

Upvotes: 2

Related Questions