2
matplotlib:颜色条及其文本标签
我想为创建colorbar图例,以heatmap使标签位于每种离散颜色的中心。从这里借来的示例: import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import ListedColormap #discrete color scheme cMap = ListedColormap(['white', 'green', 'blue','red']) #data np.random.seed(42) data = np.random.rand(4, 4) fig, ax = plt.subplots() heatmap = ax.pcolor(data, cmap=cMap) #legend cbar = plt.colorbar(heatmap) cbar.ax.set_yticklabels(['0','1','2','>3']) cbar.set_label('# of contacts', rotation=270) # put the major ticks at …