![]() ![]() 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.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. Where region_colors.values() are all unique values from your DataFrame in the form of a dictionary with their colours.I had a look into the source code of matplotlib. If you need to create a custom legend with multiple options you can use Python list comprehensions like: custom =, , marker='.', color=i, linestyle='None', markersize=25) for i in region_colors.values()] In order to plot the Scatterplot we generate 2 lists of random integers by: x = np.random.normal(0,1,15)Īnd list of random colors by: colors = Ĭustom Scatterplot legend with multiple options Next we set the legend labels, the font size and the legend position by: plt.legend(custom,, loc='upper left', fontsize=15) ![]() Is shown in the legend and the automatic mechanism described aboveīy: custom =, , marker='.', markersize=20, color='b', linestyle='None'), Use this together with labels, if you need full control on what In order to create custom legend with Matplotlib and Scatterplot we follow next steps:įirst we start with creating the legend handles which are described as:Ī list of Artists (lines, patches) to be added to the legend. Notebook Explanation of custom Scatterplot legend Plt.legend(custom,, loc='upper left', fontsize=15) import randomĬustom =, , marker='.', markersize=20, color='b', linestyle='None'), The example is showing a simple Scatterplot of few random points. In this short post you can find an example on how to add custom legend in Matplotlib and Python. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |