Home > OS >  Use Python to Create Image and Row of Boxes in Matplotlib
Use Python to Create Image and Row of Boxes in Matplotlib

Time:08-21

I'm trying to create a JPG that consists of an image stacked on top of a row of boxes. Below is a picture of what I'm trying to accomplish. Each of these boxes is an individual color.

How do I add an image above the color palette (row of colors)?

Here is the code I have tried:

palette, scores = ['#272727', '#fafafa', '#1a1a1a', '#979797', '#a1473d'], [0.75, 0.16, 0.05, 0.04, 0.0]

fig = plt.figure(figsize=(5,1))
ax = fig.add_subplot()
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)

for i, color in enumerate(palette):
    x = i/len(palette)
    rect = Rectangle((x, 0), .20, 1, facecolor=color)
    ax.add_patch(rect)

plt.savefig(os.path.join(save_dir, f'{id 1}.png'))

Here is the outcome of my code: enter image description here

Here is what I want: enter image description here

CodePudding user response:

You could use plt.GridSpec (see doc enter image description here

  • Related