I have a sample scatterplot via matplotlib via the code below.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 100, 501)
y = np.sin(x)
label = 'xy data sample'
plt.scatter(x, y, cmap='plasma', c=x, label=label)
legend_dict = dict(ncol=1, loc='best', scatterpoints=4, fancybox=True, shadow=True)
plt.legend(**legend_dict)
plt.show()
Running the code above produces the plot below.
The colormap was successfully plotted, but the legend shows points that are all blue rather than points in a color that correspond to the chosen colormap. Why does this happen?
I tried putting cmap='plasma'
in legend_dict
, but it results in the error below.
File "/Users/.../
site-packages/matplotlib/axes/_axes.py", line 550, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'cmap'
EDIT:
My desired output is to have the four dots represented in the legend to be a different color via the chosen colormap. Ideally, cmap='plasma'
in this example could produce a legend using something similar to a blue dot, then a purple dot, then an orange-red dot, then a yellow dot. Although a colorbar could make for a possible alternative, I have yet to look through any documentation about colorbars.
A colorbar can be achieved via
plt.colorbar()
. This would allow to directly see the values corresponding to the colors.Having the points in the legend show different colors is of course also nice, although it would not allow to give any quantitative information.
Unfortunately matplotlib does not provide any inbuilt way to achieve this. So one way would be to subclass the legend handler used to create the legend handle and implement this feature.
Here we create a
ScatterHandler
with a customcreate_collection
method, in which we create the desiredPathCollection
and use this by specifying it in thelegend_map
dictionary of the legend.The following code seems a bit complicated at first sight, however you may simply copy the class without understanding it completely and use it in your code.