0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.