matplotlib图片显示有重影颗粒,
import numpy as np
import matplotlib.pyplot as plt
def display_color_blocks(colors, proportions,image_path):
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
# 左侧:图片
image = Image.open(image_path)
# 在 ax1 中显示图片
ax1.imshow(image)
ax1.set_title('Local Image') # 设置子图标题
ax1.axis('off') # 关闭坐标轴
# 右侧:配色比例
for i, (color, prop) in enumerate(zip(colors, proportions)):
ax2.barh(i, prop, color=np.array(color) / 255)
ax2.text(prop / 2, i, f"{prop:.0%}",
va='center', ha='center',
color='white' if np.mean(color) < 128 else 'black')
ax2.set_xlim(0, 0.3)
ax2.set_yticks(range(len(colors)))
ax2.set_yticklabels([f"Color {i + 1}" for i in range(len(colors))])
ax2.invert_yaxis()
ax2.set_title("配色比例", fontfamily='SimHei')
plt.tight_layout()
plt.show()

