1
我覆盖补丁在与(简体)下面的代码的图像:matplotlib:匹配传奇色彩patchCollection颜色
import matplotlib.pyplot as plt
from scipy.misc import imread
from matplotlib.collections import PatchCollection
from matplotlib.patches import Circle, Arrow
import numpy as np
def plotFeatures(patches, colours, legends, str_title, colour_scale):
fig = plt.figure(); ax = plt.gca()
p = PatchCollection(patches, cmap=plt.get_cmap('Spectral_r'), alpha=0.9)
p.set_array(np.array(colours))
ax.add_collection(p)
p.set_clim(colour_scale)
fig.colorbar(p, ax=ax, fraction=0.015)
plt.xlabel(str_title)
plt.legend(handles=patches, labels=legends, bbox_to_anchor=(0., 1.02, 1., .2), mode='expand', ncol=3, loc="lower left")
# ax.set_xticks([]); ax.set_yticks([])
ax.set_xlim([0,100])
ax.set_ylim([0,100])
if __name__ == '__main__':
my_cmap = plt.get_cmap('Spectral_r')
# simplified data structure for example
allweights = [ {'name': 'Feature 1', 'mean': 2.1, 'x': 60, 'y':30},
{'name': 'Feature 2', 'mean': 3.0, 'x': 10, 'y':40},
{'name': 'Feature 3', 'mean': 2.5, 'x': 30, 'y':20} ]
KPD_patchList = []
KPD_colourList = []
KPD_legendList = []
for w in allweights:
KPD_patchList.append(Circle((w['x'], w['y']), w['mean'] + 5))
KPD_colourList.append(w['mean'])
KPD_legendList.append('{:s} ({:.2f})'.format(w['name'], w['mean']))
plotFeatures(KPD_patchList, KPD_colourList, KPD_legendList, 'myFeatures', [0, 3])
plt.show()
然而,在传说中的补丁不有正确的颜色。
我遇到的问题是我设置了PatchColelction p的颜色,但是plt.legend()不接受PatchColelction的句柄,我必须用不包含颜色数据的补丁来提供它。
我试图彩色数据直接与facecolor=my_cmap(w['mean']
增加了补丁,当我打电话Cricle,如:
for w in allweights:
KPD_patchList.append(Circle((w['x'], w['y']), w['mean'] + 5, facecolor=my_cmap(w['mean'])))
KPD_colourList.append(w['mean'])
KPD_legendList.append('{:s} ({:.2f})'.format(w['name'], w['mean']))
但随后的颜色不equaly比例为情节:
我不得不'进口matplotlib',我改变了变量'l'为'legend_handles',按[PEP8](https://www.python.org/dev/peps/pep-0008/#names-to-avoid)命名约定。否则完美。 – fuyas