많은 폴리곤 좌표가 있고 각 다각형 영역은 점수 값과 연결되어 있습니다. 히트 맵과 같은 2D 플롯을 통해 이러한 폴리곤과 관련 점수를 시각화하려고합니다. 내 코드는 이런 식으로 묘사 할 수 있지만 PatchCollection에 설정된 색상을 기반으로 색상 맵을 표시하는 방법을 찾는 데 어려움이 있습니다. 또한 Polygon 객체에 edgecolor를 지정했지만 표시되지 않습니다. 플로팅에 사용하는 데이터는 here입니다.다각형 패치 컬렉션 Matplotlib의 색상 막대 만들기 및 edgecolor 설정
import shapely
import pickle as pkl
tiles = pkl.load(open("polygons.pkl",'r'))
area_lst=[]
for tile in tiles:
area_lst.append(shapely.geometry.Polygon(tile).area)
sorted_ascend_tile_by_size= list(np.array(tiles)[np.argsort(area_lst)[::-1]])
gammas=np.random.random(len(tiles))
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
fig,ax = plt.subplots(1)
patches = []
cmap = plt.get_cmap('cool')
colors = cmap(gammas)
for tile_idx in range(len(tiles)):
polygon = Polygon(sorted_ascend_tile_by_size[tile_idx],closed=True,alpha=0.8,linewidth=1,edgecolor='black',fill=False,zorder=1)
patches.append(polygon)
collection = PatchCollection(patches)
pcollection = ax.add_collection(collection)
collection.set_color(colors)
ax.autoscale_view()
이 그 코드를 출력 플롯이다 : 나는 ScalarMappable 객체를 사용하여 년 Colorbar를 추가하려고했습니다
,하지만 난 오류 TypeError: You must first set_array for mappable
수 :
sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=0, vmax=1))
plt.colorbar(sm)