简体   繁体   中英

How to set marker type for a specific point in a matplotlib scatter plot with colormap

I have a user case that, let's say I have three series data: x,y,z. I would like to make a scatter plot using (x,y) as coordinates and z as the color of scatter points, using cmap keyword of plt.scatter. However, I would like to highlight some specific point by using a different marker type and size than other points.

​A minimum example is like below:

x,y,z = np.random.randn(3,10)
plt.scatter(x,y,c=z,cmap=matplotlib.cm.jet)
plt.colorbar()​

​If I want to use a different marker type for (x[5],y[5],z[5]), how could I do that? The only way I can think of is to plot again for this point using plt.scatter([x[5],y[5]) but define the color by manually finding the colormap ​color corresponding to z[5]. However this is quite tedious. Is there a better way?

Each scatterplot has one single marker, you cannot by default use different markers in a single scatterplot. Hence, if you are happy to only change the markersize and leave the marker the same, you can supply an array of different sizes to the scatter 's s argument.

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(10)

x,y,z = np.random.randn(3,10)

sizes = [36]*len(x)
sizes[5] = 121
plt.scatter(x,y,c=z,s=sizes, cmap=plt.cm.jet)

plt.colorbar()

plt.show()

在此输入图像描述

If you really need a different marker style, you can to plot a new scatter plot. You can then set the colorlimits of the second scatter to the ones from the first.

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(10)

x,y,z = np.random.randn(3,10)
xs, ys, zs = [x[5]], [y[5]], [z[5]]
print xs, ys, zs
y[5] = np.nan

sc = plt.scatter(x,y,c=z,s=36, cmap=plt.cm.jet)
climx, climy = sc.get_clim()
plt.scatter(xs,ys,c=zs,s=121, marker="s", cmap=plt.cm.jet, vmin=climx, vmax=climy  )  

plt.colorbar()

plt.show()

在此输入图像描述

Finally, a bit of a complicated solution to have several different markers in the same scatter plot would be given in this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM