Matplotlib subplot

while working on the Matplotlib I came across a code that was a bit confusing as I still don’t know why we are using this
f, ax = plt.subplot(figsize=(x,y))
Why are we using this for setting x-tick-labels and not simply
plt.set_xticklabels(list)
and f is being used to access axis, if someone can provide me with an answer or a guide where I can learn about this, it would be of great help, THANKS :blush:

1 Like

Without a bit more of the code it’s hard to say for certain.
Edit: Having just checked the docs there is no set_xticklabels function in the pyplot module. You can alter tick lcoations and labels with xticks but for me I’d say that it’s still less clear and more prone to risk so you should rely on methods bound to specific axes whenever possible.

matplotlib.pyplot.xticks

The following shows the arguments/returns for the xticks function. Best to stick with axis methods if you can.
locs, labels = xticks() # Get locations and labels
xticks(ticks, [labels], **kwargs) # Set locations and labels
from-
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html

Original (incorrect): plt.set_xticklabels` is a method of altering x tick labels of the current axis instance.

Difficulties can arise when it’s not clear what the current figure or current axis instance is.

I assume there’s a step that uses something like ax.set_xticklabels. It is generally good practice to use the methods associated to a particular figure or axis instance as it helps to keep track of what is being performed and when. This becomes very important when you’re working with multiple axes or multiple figures.

1 Like