I am plotting images in a loop in the following way:
plt.figure(figsize=(20, 35))
for i in range(len(batchX)): # 32 images
plt.subplot(8, 4, 1 + i)
plt.title("IMG{} - Pred: {}, GT: {}".format(i+1, classes[int(pred[i][0])], classes[int(batchy[i])]))
plt.imshow(batchX[i])
plt.tight_layout()
plt.show()
I'd like to change the background color of each plot depending on a certain condition (as you might have guessed, when the prediction equals the ground truth or not).
I tried adding this in the loop right before the imshow()
:
if classes[int(pred[i][0])] == classes[int(batchy[i])]:
plt.gcf().set_facecolor("green")
else:
plt.gcf().set_facecolor("red")
However, only the last plt.gcf()
call will be taken into account since it's when plt.show()
runs that it will look at the color setting, making all the backgrounds either green or red.
In what other way could I achieve this?