

This has the advantage that it would not use any "private" methods and works even with other objects than scatters present in the legend. Plt.legend(,, loc="lower left", markerscale=2, The only real downside is that you have to construct the legend explicitly from lists of objects and labels, but this is a well-documented matplotlib feature so it feels pretty safe to use.

This is nice because it doesn't require placing an object in your axes (potentially triggering a resize event), and it doesn't require use of any hidden attributes. You can make a Line2D object that resembles your chosen markers, except with a different marker size of your choosing, and use that to construct the legend.

But now you can use everything scatter offers. No need to touch the source, even though this is quite a hack. In this section of the tutorial, you’ll become familiar with creating basic scatter plots using Matplotlib. Now the _sizes (another underscore property) does the trick. You can use scatter plots to explore the relationship between two variables, for example by looking for any correlation between them. Lgnd = plt.legend(loc="lower left", scatterpoints=1, fontsize=10) 2 Answers Sorted by: 8 You can use seaborn's scatterplot: fig,ax plt.subplots () sns.scatterplot (datadf, hue'category', x'attempts', y'success') plt.legend (loc2) plt.savefig ('scatter.png') plt. A better hack: import matplotlib.pyplot as plt
#Legend pyplot scatter update#
It may break down at any update in matplotlib. the marker size changed manually to be 6 points for both markers in the legendĪs you can see, this utilizes hidden underscore properties ( _legmarker) and is bug-ugly.scatter changed into a plot, which changes the marker scaling (hence the sqrt) and makes it impossible to use changing marker size (if that was intended).#change the marker size manually for both lines Lgnd = plt.legend(loc="lower left", numpoints=1, fontsize=10) However, I have a hack which does probably what you want: import matplotlib.pyplot as plt The scatter plots are especially challenging in this respect. Neither of these is very much fun, though #1 seems to be easier. The transform (scaling) has to take the original size into account. Add a transform into the PathCollection objects representing the dots in the image.It is especially difficult with scatter plots ( wrong: see the update below). Bad news is that there does not seem to be any simple way of setting equal sizes of points in the legend.
#Legend pyplot scatter code#
import numpy as npĬmap = get_cmap('viridis', len(unique_ids))įor _id, color in zip(unique_ids, lors):Īx.scatter(x, y, label=_id, color=color)Īx.I had a look into the source code of matplotlib. You'll additionally need to segment a sequential colormap to achieve a non-repeating color and pair those colors against the unique IDs. This way matplotlib will infer your IDs as unique entries on your plot.

legend_elements to do this: import pandas as pdįig, ax = plt.subplots(figsize=(10, 8),dpi = 80)Īx.legend(*scatter.legend_elements(num=list(np.unique(ID))),Īx.tick_params(axis = 'x',labelrotation = 45)Īlternatively, you can iterate over your unique IDs and add each a scatter for each unique ID. You can pass the unique IDs you want a label to be created for into the num argument of. Matpotlib is currently inferring you colors to be on a continuous scale instead of a categorical one.
