Heat maps in python - with colors


I try to make a program for plotting a heat map based on the clicks from the participants. There are two bodies with increase and decrease emotions.

I want to show the intensity of clicks on the left body with blue color (more intense blue = more number of clicks) and right body with red color.

The problem is that I need to show it in one body and also with the background image over which I see the heat map.

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)

By this I get a heat map for the left body clicks, a heat map for right body clicks and a picture of the left and right bodies. I want them all in the same picture, with blue and red hotspots. I am attaching the pictures I get with this.

2 bodies picture (I have plotted blue points on it, but I do not need the points):

<img alt="2 bodies picture ( I have plotted blue points on it, but I do not need the points" class="b-lazy" data-src="https://i.stack.imgur.com/JWUZB.png" data-original="https://i.stack.imgur.com/JWUZB.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

The heat maps from the left and right bodies:

<img alt="the heat maps from the left and right bodies" class="b-lazy" data-src="https://i.stack.imgur.com/F1uj4.jpg" data-original="https://i.stack.imgur.com/F1uj4.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Please let me know if I should add more info.


You could use two different colors to represent left vs right-body clicks, and use the opacity (alpha channel) to represent the density of clicks within a given region. One way to do this would be to create a custom <a href="https://matplotlib.org/api/colors_api.html?highlight=matplotlib%20colors%20linearsegmentedcolormap#matplotlib.colors.LinearSegmentedColormap" rel="nofollow">LinearSegmentedColormap</a> that varies in the alpha channel:

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()

<a href="https://i.stack.imgur.com/0pKIP.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/0pKIP.png" data-original="https://i.stack.imgur.com/0pKIP.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Another way to achieve the same effect would be to construct two (rows, cols, 4) arrays of RGBA pixel values where the alpha channel contains your density values, and RGB contains your desired color, then imshow these on top of one another. Using a custom colormap has a few advantages though - the colormap range gets scaled automatically, and it's fairly easy to add colorbars.


  • Sklearn train model with single sample raises a DeprecationWarning
  • How to animate this 3D plot in Matlab?
  • How to implement an android delivery tracker using android studio [closed]
  • How do you generate an animated square wave from binary number for the respective decimal number usi
  • XGBoost can't find sklearn
  • Big FFT amplitude difference between the existing (synthesized) signal and the filtered signal
  • Colorbar for the point shape file
  • sklearn (Bad Input Shape) ValueError
  • Use matplotlib pick event to trigger when center of pcolormesh squares are clicked?
  • Can I get a list of wrong predictions in SVM score function in scikit-learn?
  • Heat maps in python - with colors
  • how does sklearn's Adaboost predict_proba works internally?
  • Axis2: Avoid “Unexpected subelement” error when non-required property added to WSDL
  • Unknown label type error when Sklearn naive bayes used with floating point numbers
  • How do i color the Area below the two curves?
  • Erratic data cursor behavior for triangulated 3d surfaces in MATLAB R2011b
  • Skewing an array in Python
  • How to use OpenCV in python 3.4 on windows 7 x64? [duplicate]
  • Problems using HOGDescriptor
  • Matplotlib: Custom colormap with three colors [duplicate]
  • How to deal with SpiderWebPlot in JFreeChart?
  • r - extract alphanumeric strings from text
  • K-means in OpenCV's Python interface
  • Updating one element of a bound Observable collection
  • Greek letters in legend in R
  • Need a consistent TimePicker gizmo for mobile web site
  • Geom_jitter colour based on values
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Calculating ratio of reciprocated ties for each node in igraph
  • How to create CGPath from a SKSpriteNode in SWIFT
  • Floated image with variable width and heading with background image
  • Generate random number from custom distribution
  • How to have background script and something similar to a default popup?
  • Mysterious problem with floating point in LISP - time axis generation
  • Python urlparse: small issue
  • GridView Sorting works once only
  • php design question - will a Helper help here?
  • AngularJs get employee from factory
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4