0

I have about 45 images (labelled images with numeric tags) that I want to be able to plot and save in a single piece of code. I know that overall I have 26 classes and I have a dictionary with all of them. Now, not all the images have the same tags, they will have about 50% of the tags. But I want the colours to be uniform across them. So in img1 the tag1 will be blue, img2 doesnt have any tag1, and image3 tag one will still be blue. However, I don't want the 1:blue to appear on the legend of image2.

Here's a reproducible example modified from another question:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np

##arrayLucc is the array of land use types 
arrayLucc = np.random.randint(1,4,(5,5))

## first you need to define your color map and value name as a dic
t = 1 ## alpha value
cmap = {1:[0.1,0.1,1.0,t],2:[1.0,0.1,0.1,t],3:[1.0,0.5,0.1,t], 4:[0.1, 0.2, 0.3, t]}
labels = {1:'agricultural land',2:'forest land',3:'grassland', 4:"Water"}
arrayShow = np.array([[cmap[i] for i in j] for j in arrayLucc])   

## create patches as legend
patches =[mpatches.Patch(color=cmap[i],label=labels[i]) for i in cmap]

plt.imshow(arrayShow)
plt.legend(handles=patches, loc=4, borderaxespad=0.)
plt.show()

My question is - I want to be able to remove the extra tag 4:"water" from the legend, a value that does not exist in the original arrayLucc. I have tried workarounds with np.unique, but always get the error:

'LinearSegmentedColormap' object is not subscriptable

Any thoughts?

La Cordillera
  • 410
  • 5
  • 17

0 Answers0