Legend related to span - python

I would like to add a legend which explains what each span represents in the figure and I'm having trouble to find out how to do that, since I'm new to Python/matplotlib.
So, I don't want a legend which explains each of the lines in my graphic, but I want to split the graphic using spans in different color and explain what each color (span) means.
How to do that?
I'm using this to add the spans, just to avoid confusion:
ax.axvspan(10, 300, alpha=0.2, color='red')
I'm adding an example to make things more clear. Instead of Men and Women there should be some other text and appropriate colors.

Since the legend you want does not seem refer to the actual data but some properties in your plot, I'd recommend to use pylot.text or pyplot.annotate. With these commands you can freely position your "legend".

Related

Geopandas Scheme keyword stops legend modification

New to the site and Geopandas but any advise would be good attached a pic of code and plot. It seems when you use schemes="" it creates a legend which cant be altered. If you remove this you get the normal colorbar legend . If you look at my picture What I am after is a way to make it ncol=10 so the legend goes flat and move it below the plot. Also how do you adjust the font size of title from a legend created with the schemes keyword.

Python/matplotlib plot: after changing axis labels from numbers to text, the labels are on wrong ticks

I wrote code to make a scatter plot. The x-axis ticks were originally 1 - 5 and I did the following to convert the numbers into text:
xLabels = ['V', 'rho_bd', 'lw', 'soc', 'fP']
plt.xticks(sampleId, xLabels, fontname = "Arial",fontsize=12)
Unfortunately, I have the problem shown in the figure, where the labels are on the wrong ticks. The "V" label is missing. It should be where "rho_bd" is. The "rho_bd" labels and the other labels should be moved one place to the right.
I have come across posts here where people have a problem with alignment of rotated bar graph labels to the correct ticks. It seems like that problem can be solved by simply aligning the labels--e.g., "left" instead of "right." That does not seem to be what I'm dealing with. I saw a post a while back (but can't find it again) where someone had this same problem. An answerer referred to some inherent issue with the way labels are anchored in Python. Unfortunately, that is all I recall. If there is another way to do this, I would be grateful to hear it.
I have not posted my entire code because it deals with file input and output and might be confusing. Also, I'm only allowed to provide snippets. If there is anything specific I can add that might be helpful, I would be happy to.

How to remove renderers from a plot?

I'm experimenting with Bokeh server. I have a document with three figures and I'm trying to update two of them depending on the selection I perform on the third. The number of lines to plot in the two figures changes every time.
If I could use multi_line, this would be trivial: I would change the xs and ys in the data_source of the multi_line.
Alas, I need to use multiple scatter plots because multi_line does not support hover and I need it.
So, what I would like to accomplish is to clear the two plots every time I select something in the third, and display the scatter plots corresponding to the new selection.
There are a few possible workarounds, of course (appending scatter points to have a single GlyphRenderer with all scatter plots together, for example, but this would mean using very clunky ways to send the right hover message...). But if it was possible to just clear and update single figures, everything would be cleaner. I couldn't find anything in the docs, however.
I have read the thread you created on the mailing list and this other thread where Bryan says:
Technically, glyph renderers are stored in the .renderers property of
Plots, but I would not recommend rooting around there by hand.
Specifically the "Continuous Updating" notebook I linked earlier has
an example of updating both the data and appearance of an existing
glyph using python and push_notebook. There is not any easy way to remove glyphs at the moment,
other options would be:
recreate a new plot
set the glyph to be invisble
update the glyphs data
So it seems they are the only solutions at the moment

How can I draw inline line labels in matplotlib?

I have the following graph, consisting of several lines:
Now, I would like to label all the lines in the plot. However, using legend() crams all the labels together in a box, which makes the plot somewhat difficult to interpret. What I'd like to to instead is to use inline labels. My ideal output would use labels like the following matplotlib contour plot, but with text labels for lines instead of numbers:
I haven't been able to find out how to do this in the matplotlib documentation. Is there a way to achieve this? If not, what other software could I use to generate this type of plot?
May I suggest another solution to your problem. Since in your case legend overlaps the charts you might just want to move the legend outside of the plot.
Method do move legend outside of plot is described here:
Moving matplotlib legend outside of the axis makes it cutoff by the figure box

python matplotlib blit to axes or sides of the figure?

I'm trying to refresh some plots that I have within a gui everytime I go once through a fitting procedure. Also, these plots are within a framw which can be resized, so the axes and labels etc need to be redrawn after the resizing. So was wondering if anyone knew how to update the sides of a figure using something like plot.figure.canvas.copy_from_bbox and blit. This appears to only copy and blit the background of the graphing area (where the lines are being drawn) and not to the sides of the graph or figure (where the labels and ticks are). I have been trying to get my graphs to update by trial and error and reading mpl documentation, but so far my code has jst become horrendously complex with things like self.this_plot.canvas_of_plot..etc.etc.. .plot.figure.canvas.copy_from_bbox... which is probably far too convoluted.
I know that my language might be a little off but I've been trying to read through the matplotlb documentation and the differences between Figure, canvas, graph, plot, figure.Figure, etc. are starting to evade me. So my first and foremost question would be:
1 - How do you update the ticks and labels around a matplotlib plot.
and secondly, since I would like to have a better grasp on what the answer to this question,
2 - What is the difference between a plot, figure, canvas, etc. in regards to the area which they cover in the GUI.
Thank you very much for the help.
All this can certainly be rather confusing at first!
To begin with, if you're chaining the ticks, etc, there isn't much point in using blitting. Blitting is just a way to avoid re-drawing everything if only some things are changing. If everything is changing, there's no point in using blitting. Just re-draw the plot.
Basically, you just want fig.canvas.draw() or plt.draw()
At any rate, to answer your first question, in most cases you won't need to update them manually. If you change the axis limits, they'll update themselves. You're running into problems because you're blitting just the inside of the axes instead of redrawing the plot.
As for your second question, a good, detailed overview is the Artist Tutorial of the Matplotlib User's Guide.
In a nutshell, there are two separate layers. One deals with grouping things into the parts that you'll worry about when plotting (e.g. the figure, axes, axis, lines, etc) and another that deals with rendering and drawing in general (the canvas and renderer).
Anything you can see in a matplotlib plot is an Artist. (E.g. text, a line, the axes, and even the figure itself.) An artist a) knows how to draw itself, and b) can contain other artists.
For an artist to draw itself, it uses the renderer (a backend-specific module that you'll almost never touch directly) to draw on a FigureCanvas a.k.a. "canvas" (an abstraction around either a vector-based page or a pixel buffer). To draw everything in a figure, you call canvas.draw().
Because artists can be groups of other artists, there's a hierarchy to things. Basically, something like this (obviously, this varies):
Figure
Axes (0-many) (An axes is basically a plot)
Axis (usually two) (x-axis and y-axis)
ticks
ticklabels
axis label
background patch
title, if present
anything you've plotted, e.g. Line2D's
Hopefully that makes things a touch clearer, anyway.
If you really do want to use blitting to update the tick labels, etc, you'll need to grab and restore the full region including them. This region is a bit tricky to get, because it isn't exactly known until after draw-time (rendering text in matplotlib is more complicated than rendering other things due to latex support, etc). You can do it, and I'll be glad to give an example if it's really what you want, but it's typically not going to yield a speed advantage over just drawing everything. (The exception is if you're only updating one subplot in a figure with lots of subplots.)

Categories

Resources