r/IPython • u/largelcd • Feb 09 '20
Why subplot(1,1,1) is needed? What does it mean exactly?
Hi, I know that by using subplot, we can create a figure with several subplots. Supposing that we have (2,3,1), it tells that in the figure, there are 6 subplots in the form of 2x3 (2 rows, 3 columns of subplots). The last element indicates which subplot is of interest. In this case, it is the one on the top left of the figure.
If there is only one plot in the figure, why somebody uses something like:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
What is the meaning of add_subplot(1,1,1)? Does not seem to make sense to me if there is only one plot in a figure. Thanks
2
u/TuskyMcMammoth Feb 10 '20
I think the confusion might come from the fact that there is a big distinction between
figureandaxesobjects which OP might not be aware of. Whether or not you create them explicitly yourself, afigureand at least oneaxesare always there because they handle different essential tasks related to drawing figures in matplotlib.The
figurehandles the 'picture' stuff, like how big the canvas is.The
axesis tha 'actual graph', ie the axes, axis labels, plotting space, points on the plot, etc.Calling
ax = fig.add_subplot(1,1,1)is just one available way of getting direct access to theaxesobject in your namespace under the variableax. I personally prefer the more compactfig, ax = plt.subplots(n,m)which is syntactic sugar for what OP has described.2
u/proto-n Feb 09 '20
Can't you just use
ax = plt.gca()? (as in "get current axes")1
u/iayork Feb 09 '20
I always use
subplots. It doesn’t do any harm, and even if I start with just a single plot I often end up adding others to it later on.-2
u/[deleted] Feb 09 '20
I suspect that certain routines are put into place that might not be present or accessible if going the conventional way. It might be a development relic. I do recall having to use this several times to achieve results I wanted, but didn't dig deep enough to get a full picture.
It's very unpythony, the way it is right now IMHO.