Reputation: 95
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.
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) ?
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
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