When generating a new figure or axis with matplotlib (or pyplot), there is (I assume) some sort of automated way to determine how many ticks are appropriate for each axis.
Unfortunately, this often results in labels which are too close to be read comfortably, or even overlap. I'm aware of the ways to specify tick locations and labels explicitly (e.g. ax.set_xticks, ax.set_xtick_labels, but I wonder if whatever does the automatic tick distribution if nothing is specified can be influenced by some global matplotlib parameter(s).
Do such global parameters exist, and what are they?
I'm generating lots of figures automatically and save them, and it can get a little annoying having to treat them all individually ...
In case there is no simple way to tell matplotlib to thin out the labels, is there some other workaround to achieve more generous spacing between them?
by reading the documentation of xticks matplotlib.pyplot.xticks there seems to be no such global arguments.
However it is very simple to get around it by using the explicit xticks and xticks_labels taking into account that you can:
change the font size (decrease it)
rotate the labels (by 45°) or make them vertical (less overlapping).
increase the fig size.
program a function that generates adaptif xticks based on your input.
and many other possible workarounds.
Related
I want to know how to get my x axis labels to display bigger so that the team labels aren't overlapping. I'm sure it's just a matter of configuring the chart size
My code:
plt.plot(prem_data.Team, prem_data.attack_scored,'o')
plt.plot(prem_data.Team, prem_data.defence_saves)
plt.xlabel("Team")
plt.ylabel("Attack goals scored & Defence tackles")
plt.legend(["attack scored", "defence saved"])
plt.show()
I can imagine there being two mutually non-exclusive solutions.
Directly alter the size of the font. This can be achieved via calling plt.rcParams.update({'font.size': <font_size>}), assuming that you have imported matplotlib.pyplot under the alias plt, as you have done in the source code provided. You would probably want to set the <font_size> to be small to prevent overlapping labels, but this would require some experimentation.
Increase the size of the figure. This can be done in a number of ways, but perhaps the simplest method you can implement with minimal edits to your current code would be to use the command plt.rcParams["figure.figsize"] = <fig_size> where <fig_size> is a tuple specifying the size of the figure in inches, such as (10, 5).
With some trial and error, you should be able to manipulate the size of the font and the figure to produce a plot with improved readability.
Note: The method for altering figure size I introduced above is not the most conventional way to go about this problem. Instead, it is much more common to use matplotlib.pyplot.figure or similar variants. For more information, I recommend that you check out this thread and the documentation.
What is the best way to set the default gid for plot elements in matplotlib? I want to completely get rid of the unpredictable gid's when saving svg figures, and set them to sensible, predictable values such as 'xaxis', 'xtick1', 'xtick2', 'curve1', 'legend1' etc. Without having lots of boilerplate code for each separate figure, of course.
I would be happy to subclass the relevant part of matplotlib and write code for this, but matplotlib is pretty big and I am not sure where to start.
It's a simple thing but I've searched for quite a while without success: I want to customise a figure legend by reversing the horizontal order of the symbols and labels.
In Gnuplot, this is simply achieved by set key reverse. Example: change x data1 to data1 x. In matplotlib, there seems to be no user-friendly solution. Thus, I thought about changing a kind of handle anchor or just shifting the handle's position, but couldn't find any point to start with.
The requested feature is already there, as the keyword markerfirst of the legend command.
plt.plot([1,2],[3,4], label='labeltext')
plt.legend(markerfirst=False)
plt.show()
If you want to make it your default behaviour, you can change the value of legend.markerfirst in rcParams, see customizing matplotlib.
Is there a command to set the length of an axis? I do not mean the range! Independently from the values, the range from the axis or other factors, I want to set its length. How can I do that?
Something like plt.yaxislenght(20)?
I'm not sure of a specific way to set an axis length of axes generated by e.g. plt.subplots. You can use ax.set_aspect(num), but this adjusts the aspect ratio, and therefore will change both axes in a dependent way.
You can however use ax = plt.axes([left,bottom,width,height]) to add individual subplots in whatever positions you like. This should allow you to achieve what you want, but will be tedious because you need to place each subplot manually.
What you want to do is tricky due to the way that mpl works underneath. Most of the artist are specified in units that are not on-screen units (data, axes, or figure space: see transfrom tutorial). This gives you a good deal of power/flexibility as most of the time you want to work in one of the relative sets of coordinates, however the cost is if you want to set 'absolute' sizes of things you end up having to do it indirectly.
If you want you axis to be a fixed length (in display units) between figures, then you need to control the size of you axes (in figure units) by hand (via fig.add_axes) and then use fig.set_size_inches to set the size of your over-all figure. By tweaking these values you can get what you want.
I am trying to minimize margins around a 1X2 figure, a figure which are two stacked subplots. I searched a lot and came up with commands like:
self.figure.subplots_adjust(left=0.01, bottom=0.01, top=0.99, right=0.99)
Which leaves a large gap on top and between the subplots. Playing with these parameters, much less understanding them was tough (things like ValueError: bottom cannot be >= top)
My questions :
What is the command to completely minimize the margins?
What do these numbers mean, and what coordinate system does this follow (the non-standard percent thing and origin point of this coordinate system)? What are the special rules on top of this coordinate system?
Where is the exact point this command needs to be called? From experiment, I figured out it works after you create subplots. What if you need to call it repeatedly after you resize a window and need to resize the figure to fit inside?
What are the other methods of adjusting layouts, especially for a single subplot?
They're in figure coordinates: http://matplotlib.sourceforge.net/users/transforms_tutorial.html
To remove gaps between subplots, use the wspace and hspace keywords to subplots_adjust.
If you want to have things adjusted automatically, have a look at tight_layout
Gridspec: http://matplotlib.sourceforge.net/users/gridspec.html