【问题标题】:Heat maps in python - with colorspython中的热图 - 带有颜色
【发布时间】:2018-01-23 16:19:00
【问题描述】:

我尝试制作一个程序,用于根据参与者的点击绘制热图。有增加和减少情绪的两个身体。

我想用蓝色(更强烈的蓝色 = 更多的点击次数)和红色的右体显示点击强度。

问题是我需要将它显示在一个主体中,并且还需要与我看到热图的背景图像一起显示。

x=blue[:,1]
y=blue[:,2]
ax = plt.gca()

heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.imshow(heatmap.T, extent=extent, origin='lower')
ax = plt.gca()
ax.invert_yaxis()

plt.show()

x1=red[:,1]
y1=red[:,2]
ax = plt.gca()

heatmap, xedges, yedges = np.histogram2d(x1, y1, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

plt.clf()
plt.imshow(heatmap.T, extent=extent, origin='lower')
ax = plt.gca()
ax.invert_yaxis()

plt.show()
plt.imshow(image)

imageFile = cbook.get_sample_data('C:\\Users\\Admin\\Desktop\\pythonpca\\result.png')
image = plt.imread(imageFile)
plt.plot(all_samples[0:240,0],all_samples[0:240,1], 'o', markersize=3, color='blue', alpha=0.5, label='increase')
plt.imshow(image)

通过这个,我得到了左侧身体点击的热图、右侧身体点击的热图以及左右身体的图片。我希望它们都在同一张图片中,带有蓝色和红色的热点。我正在附上我得到的图片。

2张尸体图片(我在上面画了蓝点,但我不需要这些点):

左右身体的热图:

如果我应该添加更多信息,请告诉我。

【问题讨论】:

标签: python matplotlib heatmap


【解决方案1】:

您可以使用两种不同的颜色来表示左侧和右侧的点击,并使用不透明度(Alpha 通道)来表示给定区域内的点击密度。一种方法是创建一个在 alpha 通道中变化的自定义 LinearSegmentedColormap

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import colors

# Some fake data
nbins = (50, 50)
ranges = ((-2, 2), (-2, 2))
lx, ly = np.random.randn(2, 10000) * 0.5 + 1
rx, ry = np.random.randn(2, 10000) * 0.75 - 1

# Compute 2D histograms
left_density = np.histogram2d(lx, ly, bins=nbins, range=ranges, normed=True)[0]
right_density = np.histogram2d(rx, ry, bins=nbins, range=ranges, normed=True)[0]

# Make some custom colormaps that vary in the alpha channel.
trans2blue = colors.LinearSegmentedColormap.from_list(
    name='Trans2Blue', colors=[(0., 0., 1., 0.), (0., 0., 1., 1.)])
trans2red = colors.LinearSegmentedColormap.from_list(
    name='Trans2Red', colors=[(1., 0., 0., 0.), (1., 0., 0., 1.)])

# `imshow` the histograms using the custom colormaps.
fig, ax = plt.subplots(1, 1)
left_im = ax.imshow(left_density, cmap=trans2blue)
right_im = ax.imshow(right_density, cmap=trans2red)

right_cb = fig.colorbar(right_im)
right_cb.set_label('Right click density')
left_cb = fig.colorbar(left_im)
left_cb.set_label('Left click density')

# Workaround for https://stackoverflow.com/q/15003353/1461210
left_cb.solids.set_edgecolor("face")
right_cb.solids.set_edgecolor("face")

fig.tight_layout()

实现相同效果的另一种方法是构造两个(rows, cols, 4) RGBA 像素值数组,其中 alpha 通道包含您的密度值,RGB 包含您想要的颜色,然后 imshow 将它们叠加在一起。不过,使用自定义颜色图有一些优点 - 颜色图范围会自动缩放,并且添加颜色条相当容易。

【讨论】:

  • 非常感谢!
  • @ArunSubraminion 很高兴为您提供帮助。如果它解决了您的问题,请不要忘记接受答案。
  • 请问如何给这个添加背景图片?另外,如果我想根据参与者单击的位置将 PCA 添加到该程序中,以便区分不同的测试用例,我该怎么做?我的意思是我想找出人们点击的区域是奇怪的情绪。
  • 由于覆盖的直方图图像是半透明的,您可以在imshow将您的两个热图数组放在顶部之前,在同一组轴中 imshow 您的背景图像。如何执行 PCA 确实是一个完全独立的问题,因此我不会尝试在此处的 cmets 中回答它。在您打开一个新问题之前,请先尝试在 StackOverflow 上进行搜索,因为已经有很多现有答案解释了在 Python 中执行 PCA 的各种方法,例如herehere.
猜你喜欢
  • 2022-12-16
  • 1970-01-01
  • 2016-07-20
  • 2019-03-08
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多