Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove line through marker in matplotlib legend

I have a matplotlib plot generated with the following code:

import matplotlib.pyplot as pyplot  Fig, ax = pyplot.subplots() for i, (mark, color) in enumerate(zip(     ['s', 'o', 'D', 'v'], ['r', 'g', 'b', 'purple'])):     ax.plot(i+1, i+1, color=color,             marker=mark,             markerfacecolor='None',             markeredgecolor=color,             label=i)  ax.set_xlim(0,5) ax.set_ylim(0,5) ax.legend() 

with this as the generated figure: matplotlib generated figure

I don't like the lines through the markers in the legend. How can I get rid of them?

like image 435
jlconlin Avatar asked Jan 22 '14 14:01

jlconlin


People also ask

How do I make markers transparent in matplotlib?

How to make the marker face color transparent without making the line transparent in Matplotlib? Create x_data and y_data(sin(x_data)), using numpy. Plot curve using x_data and y_data, with marker style and marker size. By changing the alpha, we can make it transparent to opaque.

What happens if I dont use %Matplotlib inline?

In the current versions of the IPython notebook and jupyter notebook, it is not necessary to use the %matplotlib inline function. As, whether you call matplotlib. pyplot. show() function or not, the graph output will be displayed in any case.

What is %Matplotlib inline?

%matplotlib inline is an example of a predefined magic function in Ipython. They are frequently used in interactive environments like jupyter notebook. %matplotlib inline makes your plot outputs appear and be stored within the notebook.


2 Answers

You can specify linestyle="None" as a keyword argument in the plot command:

import matplotlib.pyplot as pyplot  Fig, ax = pyplot.subplots() for i, (mark, color) in enumerate(zip(     ['s', 'o', 'D', 'v'], ['r', 'g', 'b', 'purple'])):     ax.plot(i+1, i+1, color=color,             marker=mark,             markerfacecolor='None',             markeredgecolor=color,             linestyle = 'None',             label=`i`)  ax.set_xlim(0,5) ax.set_ylim(0,5) ax.legend(numpoints=1) pyplot.show() 

enter image description here

Since you're only plotting single points, you can't see the line attribute except for in the legend.

like image 189
tom10 Avatar answered Sep 20 '22 17:09

tom10


You can set the rcparams for the plots:

import matplotlib matplotlib.rcParams['legend.handlelength'] = 0 matplotlib.rcParams['legend.numpoints'] = 1 

enter image description here

All the legend.* parameters are available as keywords if you don't want the setting to apply globally for all plots. See matplotlib.pyplot.legend documentation and this related question:

legend setting (numpoints and scatterpoints) in matplotlib does not work

like image 27
Hooked Avatar answered Sep 19 '22 17:09

Hooked