gpt4 book ai didi

python - Matplotlib PatchCollection 与 set_array : how to match face-color with edge-color?

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:07 25 4
gpt4 key购买 nike

我编写了一个Python代码,可以生成不同尺寸的矩形。但是,我无法使边缘颜色与面部颜色相匹配。这是我的代码的相关部分:

# Mapping row information to geometry and color of plot points
x = np.array(info[0])-time
y = np.array(info[1])
x_width = np.array(info[2])
y_width = np.array(info[3])
colors = np.array(info[4])
colors = np.log10(colors)

patches = []

# Save rectangle object-datapoints to patches
(time_min,time_max) = (x-x_width/2., x+x_width/2.)
(freq_min, freq_max) = (y-y_width/2., y+y_width/2.)

for i in range(len(x)):
f_min_plot = max(freq_min[i],f_min)
patches.append(Rectangle((time_min[i], f_min_plot), x_width[i], freq_max[i]-f_min_plot))

fig = pl.figure()
pl.rc('text', usetex=True)
ax = fig.add_subplot(111)
p = PatchCollection(patches, match_original=False)
p.set_array(colors)
ax.add_collection(p)
cbar = pl.colorbar(p)
#cbar.set_label('$\log_{10}$ SNR', rotation = 270)
pl.xlabel("Time-%.1f" %(time))
pl.ylabel("Frequency")
pl.semilogy()
pl.title("%s1 omicron triggers"%(detector))
pl.xlim(-time_interval,time_interval)
pl.ylim(20,2000)
pl.savefig("%s/%s1-omicron-trigs-%.1f-%d.png" %(file_path, detector, time, time_interval))

此代码生成带有黑色边框的默认矩形面颜色。我需要边框颜色与面部颜色相匹配。请注意,我尝试使用 edgecolor =“none”删除边框。不幸的是,这使得查看某些矩形(宽度较小的矩形)的颜色变得相当困难。我还尝试在 Rectangle() 语句中添加 color = matplotlib.cm.jet(color_norm[i] # color_norm is the "normalized"color array: color/max(color)) (当然还有 PatchCollections 中的 match_original = True () 陈述)。但这也行不通。在我看来, set_array() 最终决定了颜色。我找不到任何关于 set_array 的确切用途以及如何使用它的详细文档。

有什么方法可以让这段代码绘制具有相同面部颜色和边缘颜色的矩形吗?我真诚地感谢您对这个问题的任何帮助。

最佳答案

您可以通过 'face' 关键字使用 set_edgecolor 命令,如下所示:

p.set_edgecolor('face')

这对我来说很有效,并将边缘颜色设置为与面部颜色相同的值。

关于python - Matplotlib PatchCollection 与 set_array : how to match face-color with edge-color?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23738732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com