Accessing Axes.tick_params via plt.tick_params

Screen Link:
https://app.dataquest.io/m/147/improving-plot-aesthetics/6/hiding-tick-marks

My Code:

plt.plot(women_degrees['Year'], women_degrees['Biology'], color="blue", label="Women")
plt.plot(women_degrees['Year'], 100-women_degrees['Biology'], color="green", label="Men")

plt.tick_params(bottom="off", top="off", left="off", right="off")

plt.title("Percentage of Biology Degrees Awarded By Gender")
plt.legend(loc="upper right")
plt.show()

What I expected to happen:
My code worked and I wasn’t expecting it to…I used plt.tick_params because I didn’t have an Axes object created and I was curious to see if it worked. What I am confused about is why my code worked. I don’t understand how Matplotlib was able to access the tick_params() method when there wasn’t an instance of the Axes Class initiated.

Also after comparing the answer to my code, I see I was able to further access other methods (‘legend’, ‘title’) that are also part of the Axes class. Just a little confused about how I should be writing my code–whether initiating objects or simply accesses the same methods/attributes from plt.

TL;DR:Looking for some help understanding the way Matplotlib works with the the underlying Figure and Axes Classes and why I was able to access this method with no object.

What actually happened:

Replace this line with the output/error

Hey @ryan.pikulski, actually there is no difference between axes.tick_params and plt.tick_params. Is just a matter of best practice.

To make myself more clearer both ways are well written is just that creating axes is a must when you want to plot into multiple axes in one figure, like subplots

1 Like

@alegiraldo666 thank you for confirming. If I understand correctly, Using Axes Class would give me more control and specific access to individual axes if I was using multiple subplots.

In this case I am using a single plot, which is why accessing the methods works through plt?

2 Likes

@ryan.pikulski exactly. If you want to understand a bit more check matplotlib’s usage guide

1 Like