AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Legend pyplot scatter8/19/2023 The key is to map the scatter PathCollection to a HandlerPathCollection with an updating function being set to it. 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. Now the _sizes (another underscore property) does the trick. Lgnd = plt.legend(loc="lower left", scatterpoints=1, fontsize=10) A better hack: import matplotlib.pyplot as plt 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.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. 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) 5 Answers Sorted by: 103 I had a look into the source code of matplotlib.#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). Plt.scatter(group.x, group.y, s=sizes, alpha=0.I had a look into the source code of matplotlib. Labels = įor i, (name, group) in enumerate(grouped): Grouped = df.groupby(np.digitize(df.a2, bins)) # Create the DataFrame from your randomised data and bin it using groupby.ĭf = pd.DataFrame(data=dict(x=x, y=y, a2=a2))īins = np.linspace(df.a2.min(), df.a2.max(), M) Using this method you could vary other parameters for each bin, such as the marker shape or colour. You can always increase the number of bins to make it finer as suits you. Note this is slightly different to your stated problem as the marker sizes are binned, this means that two elements in a2, say 36 and 38, will have the same size as they are within the same binning. I have used the binning recipe from this question. It plots each group and assigns it a label and a size for the markers. The solution below used pandas to group the sizes together into set bins (with groupby).
0 Comments
Read More
Leave a Reply. |