要获得图例,您需要在至少一个数据点上调用scatter时传递关键字label。一种方法是从数据中选择3个具有代表性的点,然后用标签将它们再次添加到绘图中。在import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0) # so the image is reproducible
x1 = np.random.rand(30)
y1 = np.random.rand(30)
z1 = np.random.rand(30)
fig = plt.figure(figsize=(8,6))
inset = fig.add_subplot(111)
# i prefer no outlines on the dots, so edgecolors='none'
axis = inset.scatter(x1,y1,s=z1*100,c=z1,vmin=0,vmax=1,edgecolors='none')
inset.set_xlabel("X axis")
inset.set_ylabel("Y axis")
cbar = fig.colorbar(axis,ticks=[0,0.5,1])
cbar.ax.set_yticklabels(["Low","Medium","High"])
# here we step over the sorted data into 4 or 5 strides and select the
# last 3 steps as a representative sample,