gpt4 book ai didi

python - 如何使用 hexbin 和 matplotlib 将颜色淡化为透明?

转载 作者:太空宇宙 更新时间:2023-11-03 11:27:15 24 4
gpt4 key购买 nike

我正在使用 hexbin 创建“热图”,并希望将此热图放置在图像之上。但是,我想让绘图的颜色像频率一样淡化为透明(即,随着颜色淡化为白色,它会消失)。我试过更改 alpha 值,但没有产生预期的效果。

我的代码是:

n = 100000
x = np.random.standard_normal(n)
img = imread("soccer.jpg")
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
plt.hexbin(x,y, bins='log', cmap=plt.cm.Reds, alpha = 0.3)
plt.imshow(img,zorder=0, extent=[-10, 10, -20, 20])
#plt.show()
plt.savefig('map.png')

我愿意使用二维直方图或任何其他绘图函数。即使在该六边形中没有任何值时保持透明也会很棒,因为我的许多区域的数据点为零。

我当前代码生成的图像是:

Heatmap image

最佳答案

继续使用的粗略示例:

n = 100000
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap


x = np.random.standard_normal(n)
img = plt.imread("soccer.jpg")
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)

red_high = ((0., 0., 0.),
(.3, .5, 0.5),
(1., 1., 1.))

blue_middle = ((0., .2, .2),
(.3, .5, .5),
(.8, .2, .2),
(1., .1, .1))

green_none = ((0,0,0),(1,0,0))

cdict3 = {'red': red_high,

'green': green_none,

'blue': blue_middle,

'alpha': ((0.0, 0.0, 0.0),
(0.3, 0.5, 0.5),
(1.0, 1.0, 1.0))
}


dropout_high = LinearSegmentedColormap('Dropout', cdict3)
plt.register_cmap(cmap = dropout_high)

plt.hexbin(x,y, bins='log', cmap=dropout_high)
plt.imshow(img,zorder=0, extent=[-10, 10, -20, 20])
plt.show()
#plt.savefig('map.png')

enter image description here

(恐怕我的足球场是侧边的,我平时踢的好像是侧边的,所以。)

关于python - 如何使用 hexbin 和 matplotlib 将颜色淡化为透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575400/

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