[英]Use Python to Create Image and Row of Boxes in Matplotlib
I'm trying to create a JPG that consists of an image stacked on top of a row of boxes.我正在尝试创建一个 JPG,该 JPG 由堆叠在一排盒子顶部的图像组成。 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'))
You could use plt.GridSpec
(see doc here ).您可以使用
plt.GridSpec
(请参阅此处的文档)。
See code below:请参见下面的代码:
import matplotlib.pyplot as plt
palette, scores = ['#272727', '#fafafa', '#1a1a1a', '#979797', '#a1473d'], [0.75, 0.16, 0.05, 0.04, 0.0]
fig = plt.figure(figsize=(6,10))
gs=plt.GridSpec(8,3)
ax=plt.subplot(gs[7:8,0:3])
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
for i, color in enumerate(palette):
x = i/len(palette)
rect = plt.Rectangle((x, 0), .20, 1, facecolor=color)
ax.add_patch(rect)
plt.box(False)
ax2=plt.subplot(gs[0:7,0:3])
img=plt.imread('b.jpg')
ax2.imshow(img,aspect='auto')
ax2.axes.xaxis.set_visible(False)
ax2.axes.yaxis.set_visible(False)
plt.box(False)
plt.tight_layout()
plt.show()
And the output returns the following: output 返回以下内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.