matplotlib: put legend symbols on the right of the labels - python

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.

Related

Get coordinates/limits of AnchoredText

I set up a figure with multiple axes, where some axes have an AnchoredText. I want to add a ConnectionPatch to connect one axis to an AnchoredText in another axis. While I can get the limits of the axis, I'm not sure if something similar can be done for an AnchoredText.
I have looked through the properties of AnchoredText, Offsetbox, and Artist, and have tried a couple of methods but haven't found anything that gives me anything close to what I'm looking for. At best, I can only guess, although I want to be able to code this in a way in which it can be done dynamically.

Matplotlib: increase distance between automatically generated tick labels

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.

Add Label Annotations on Axes

I currently annotate my charts with the last value of each series by adding a Label and supplying my the name of corresponding range it's plotted on:
Label(
...
x=data.index.max(),
y=data.loc[data.index.max(), 'my_col'],
y_range_name='my_range'
...
)
Which gives me:
How do I move the labels so they are positioned on their respective axis?
Example:
Please note that my labels' y-positioning is off, so I need some help with that aspect too. I've tried tweaking the y_offset but this has not yielded any consistently good results.
My data are always numerical time series.
As of Bokeh 1.2 there is no built-in annotation or glyph that will display outside the central plot area. There is an open issue on GitHub that this is similar to that you can follow or comment on. For the time being, something like this would require making a custom extension

Off-center X-Axis in Seaborn

I'm having an issue getting my boxplot to align with my x axis labels. I've tried adjusting the size of the chart, but the data points still look a little off. I appreciate any help!
This is the current chart:
It's hard to tell without an MCVE, But I'm guessing it's because you're using two categorical variables; x, and hue. This creates a so called "nested" (search for the key-word "smoke") box-plot, and if one of the categories is empty in some sense might cause the observed off-set.
Again, only guessing 'cause that's what you gave us.
Good luck!
This misalignment can happen when the hue argument is set.
You can add the dodge=False argument to the sns.boxplot function to keep boxplots aligned with the x-axis labels.
In your example, it would look like this:
sns.boxplot(x=df["Groups"], y=df["Rate per Month"], hue=df["Hours per Month"], dodge=False)
Description of the dodge parameter from the the seaborn.boxplot documentation:
dodge: bool, optional
When hue nesting is used, elements should be shifted along the categorical axis.
Example from the seaborn.boxplot documentation.

Multiple tick locators on single axis of a plot in matplotlib

I've done some searching around, and cannot easily find a solution this problem. Effectively, I want to have multiple tick locators on a single axis such that I can do something like in the plot below.
Note how the x-axis starts off logarithmic, but becomes linear once 500 is reached. I figured one possible solution was to simply divide the data into two portions, plot it on two graphs, each with their own locators, and then put the graphs right next to each other so they're seamless, but that seems very unpythonic. Anyone have a better solution?
I suspect the following URL might be of use:
http://matplotlib.org/examples/axes_grid/parasite_simple2.html (click on the plot to have the python code)
If you need some specialized graphs, it's always a good idea to have a look at the Matplotlib gallery:
http://matplotlib.org/gallery.html
EDIT: It is possible to make custom ticks on the X-axis:
http://matplotlib.org/examples/ticks_and_spines/ticklabels_demo_rotation.html
You may find an implementation of this scale by Jesús Torrado here.

Categories