Apologies for the really long set of questions.
I am trying to plot a graph in matplotlib. I was faced with this issue of limiting the number of ticks on both of the axes. Looking into pyplot I could not find any solution.
The only solution I came across was by creating a subplot in the following manner.
ax = plt.subplot(111)
ax.xaxis.set_major_locator(plt.MaxNLocator(4))
Although the above works, I am left with a few unsolved questions most of them in relation to how the matplotlib library is structured.
Is there no feature whereby an object of pyplot.plot() can have
the number of ticks limited. Do I have to always depend on
subplotting?
When i create an object ax = plt.subplot(111) I find that it
creates an instance as below
type(ax)
Out[228]: matplotlib.axes._subplots.AxesSubplot
Why does the documentation say that the subplot method returns a class ---> axes.SubplotBase
Also I see that we need to use the xaxis attribute of ax(is it a method) which helps set the property related to the ticks.
type(ax.xaxis)
Out[233]: matplotlib.axis.XAxis
When ax is an object of some subclass of matplotlib.axes (not sure if it is SubplotBase or AxesSubplot) how come we can refer to ax.xaxis. The xaxis (or axis.Xaxis) attribute is not mentioned under to documentation of the matplotlib.axes.
I am pretty confused over the hierarchy and structure of matplotlib. It would be be helpful if someone can point me to an article or blog which details the structure of these features.
Looking through the documentation I could not figure out a suitable attribute of the subplot class which could help solve this problem related to number of ticks. I am not sure how I am going to solve the next problem if I cant go through the documentation and figure it out.
Thanks,
Sree
Related
I am trying to understand how matplotlib works (my question is probably more general though).
To plot a curve, I can do either of the following:
fig,ax=plt.subplots()
plt.plot([1,2,3])
#%%
fig,ax=plt.subplots()
ax.plot([1,2,3])
In the first case, python knows that the plot should be done with respect to the set of axes "ax". In the second case I "tell it" in a way that it is for the set of axes "ax" because I use the method from ax.
First question:
How does the first way works ? How does python knows that the plot has to be done with ax ? What is the mechanics behind this ? Indeed I would expect that I have to tell it on what to plot. But it "deduces" it.
Second question:
As a related question: is there anything different between the two ways of plotting or are they totally equivalent ?
Third question:
plt.plot has an "analog method" with the axes: ax.plot
plt.pcolor has an "analog method" with the axes: ax.pcolor
We can find many other examples for that.
Is it true for all plotting method ? Inside of matplotlib is it indeed exactly the same code behind plt.FUNCTION and ax.FUNCTION (using the same "FUNCTION" for both)?
Though I've been able to create basic plots with matplotlib.pyplot before, I cannot fully understand what the difference between these are:
figure
subplot
axes
(axis?)
As I understand, all three of these are objects for the area in which a graph is drawn, so what distinguishes them in terms of what can be drawn inside them?
Matplotlib's user guide gives a great description about the parts of a figure.
The components are hierarchical:
Figure object: the whole figure
Axes object: belongs to a Figure object and is the space where we add the data to visualize
Axis object: belongs to an Axes object. Axis object can be categorized as XAxis or YAxis.
For further info, visit this link:
Getting Started with Matplotlib
I'm not really new to matplotlib and I'm deeply ashamed to admit I have always used it as a tool for getting a solution as quick and easy as possible. So I know how to get basic plots, subplots and stuff and have quite a few code which gets reused from time to time...but I have no "deep(er) knowledge" of matplotlib.
Recently I thought I should change this and work myself through some tutorials. However, I am still confused about matplotlibs plt, fig(ure) and ax(arr). What is really the difference?
In most cases, for some "quick'n'dirty' plotting I see people using just pyplot as plt and directly plot with plt.plot. Since I am having multiple stuff to plot quite often, I frequently use f, axarr = plt.subplots()...but most times you see only code putting data into the axarr and ignoring the figure f.
So, my question is: what is a clean way to work with matplotlib? When to use plt only, what is or what should a figure be used for? Should subplots just containing data? Or is it valid and good practice to everything like styling, clearing a plot, ..., inside of subplots?
I hope this is not to wide-ranging. Basically I am asking for some advice for the true purposes of plt <-> fig <-> ax(arr) (and when/how to use them properly).
Tutorials would also be welcome. The matplotlib documentation is rather confusing to me. When one searches something really specific, like rescaling a legend, different plot markers and colors and so on the official documentation is really precise but rather general information is not that good in my opinion. Too much different examples, no real explanations of the purposes...looks more or less like a big listing of all possible API methods and arguments.
pyplot is the 'scripting' level API in matplotlib (its highest level API to do a lot with matplotlib). It allows you to use matplotlib using a procedural interface in a similar way as you can do it with Matlab. pyplot has a notion of 'current figure' and 'current axes' that all the functions delegate to (#tacaswell dixit). So, when you use the functions available on the module pyplot you are plotting to the 'current figure' and 'current axes'.
If you want 'fine-grain' control of where/what your are plotting then you should use an object oriented API using instances of Figure and Axes.
Functions available in pyplot have an equivalent method in the Axes.
From the repo anatomy of matplotlib:
The Figure is the top-level container in this hierarchy. It is the overall window/page that everything is drawn on. You can have multiple independent figures and Figures can contain multiple Axes.
But...
Most plotting occurs on an Axes. The axes is effectively the area that we plot data on and any ticks/labels/etc associated with it. Usually we'll set up an Axes with a call to subplot (which places Axes on a regular grid), so in most cases, Axes and Subplot are synonymous.
Each Axes has an XAxis and a YAxis. These contain the ticks, tick locations, labels, etc.
If you want to know the anatomy of a plot you can visit this link.
I think that this tutorial explains well the basic notions of the object hierarchy of matplotlib like Figure and Axes, as well as the notion of current figure and current Axes.
If you want a quick answer: There is the Figure object which is the container that wraps multiple Axes(which is different from axis) which also contains smaller objects like legends, line, tick marks ... as shown in this image taken from matplotlib documentation
So when we do
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> type(fig)
<class 'matplotlib.figure.Figure'>
>>> type(ax)
<class 'matplotlib.axes._subplots.AxesSubplot'>
We have created a Figure object and an Axes object that is contained in that figure.
pyplot is matlab like API for those who are familiar with matlab and want to make quick and dirty plots
figure is object-oriented API for those who doesn't care about matlab style plotting
So you can use either one but perhaps not both together.
I would like to know if there is a way to combined several figures created with matplotlib in one unique figure.
Most of the existing topics are related to multiple plots within one figure. But here, I have several functions which all create one elaborated figure (not just a plot, the figure itself is a multiple plot with texts, title, legends,...)
So instead of just doing the layout of those several figures using a software like Word, is there a way to directly combined all my figures in one unique figure under python ?
Thank you in advance !
The concept of figure in matplotlib does not allow to have a figure inside a figure. The figure is the canvas for other artists, like axes. You may of course add as many axes to a figure as you like. So for example instead of one figure with 4 axes and another figure with 6 axes, you can create a figure with 10 axes.
A good choice may be to use the gridspec, as detailed on the respecive matplotlib page.
After additional researches, it seems my problem has no easy solution within Matplotlib itself. Multiple figures layout needs external post-processing of plots.
For those having the same problem, here is an interesting link :
Publication-quality figures with matplotlib and svgutils
I have a DataFrame(called result_df) and want to plot one column with boxplot.
But certain outliers spoiled the visualization. How could I prevent from ploting outliers?
Code I used:
fig, ax = pl.subplots()
fig.set_size_inches(18.5,10.5)
result_df.boxplot(ax=ax)
pl.show()
Important: I haven't paid enough attention, apparently that happens a lot, and I missed that it's pandas specific. However from questions I saw it's basically matplotlib for graphing in the background so this could still work. Sorry I failed to be more careful.
Luckily for you there is such a thing. In the manual under results: dict title torwards the bottom of the page it states:
fliers: points representing data that extend beyond the whiskers
(outliers).
Setting showfliers=False will hopefully help you.
I do have to mention though, that I find it really really strange they shortened outliers to fliers. If that doesn't help manual offers a second solution:
sym : str or None, default = None
The default symbol for flier points. Enter an empty string (‘’) if you don’t want to show fliers. If None, then the fliers default to
‘b+’ If you want more control use the flierprops kwarg.