Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I draw inline line labels in matplotlib?

I have the following graph, consisting of several lines:

Sample line plot

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:

Sample contour plot

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?

like image 430
Daniel Avatar asked Feb 04 '14 11:02

Daniel


People also ask

How do I draw a linear line in matplotlib?

Matplotlib: Graph/Plot a Straight Line The equation y=mx+c y = m x + c represents a straight line graphically, where m is its slope/gradient and c its intercept.

How do you label a vertical line in Python?

Using vline(), axvline(), and plot are some of the matplotlib pyplot functions used to insert vertical lines.

Do You Need %Matplotlib inline?

The only reason %matplotlib inline is used is to render any matplotlib diagrams even if the plt. show() function is not called. However, even if %matplotlib inline is not used, Jupyter will still display the Matplotlib diagram as an object, with something like matplotlib. lines.

Why do we write matplotlib inline?

The line magic command %matplotlib inline enables the drawing of matplotlib figures in the IPython environment. Once this command is executed in any cell, then for the rest of the session, the matplotlib plots will appear directly below the cell in which the plot function was called.


1 Answers

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

like image 196
jb. Avatar answered Oct 13 '22 14:10

jb.