× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

In matlab, how to 'smooth' pixels in 2D heatmap using imagesc

Ray Phan
Jul 02, 2015
<p><a href="http://www.mathworks.com/help/matlab/ref/interp2.html" rel="nofollow"><code>interp2</code></a> may be of use here. Use the data as key points, then create a finer grid of points that span the same width and height and interpolate in between the key points. </p> <p>Something like this:</p> <pre><code>%// Define your data data = [1 1 1 1 1 1 1 1 1 1; 1 1.04 1.04 1.04 1.03 1 1.01 1.01 1.03 1.01; 1.36 1.3 1.25 1.2 1.15 1.1 1.2 1.13 1.07 1.11; 3.65 3.16 2.94 2.68 2.39 2.22 2.17 1.95 1.79 1.81; 5.91 5.75 5.47 5.3 4.98 4.79 4.62 4.55 4.38 4.19; 6 6 5.99 5.83 5.49 5.33 5.14 4.94 4.77 4.74]; %// Define integer grid of coordinates for the above data [X,Y] = meshgrid(1:size(data,2), 1:size(data,1)); %// Define a finer grid of points [X2,Y2] = meshgrid(1:0.01:size(data,2), 1:0.01:size(data,1)); %// Interpolate the data and show the output outData = interp2(X, Y, data, X2, Y2, 'linear'); imagesc(outData); %// Cosmetic changes for the axes set(gca, 'XTick', linspace(1,size(X2,2),size(X,2))); set(gca, 'YTick', linspace(1,size(X2,1),size(X,1))); set(gca, 'XTickLabel', 1:size(X,2)); set(gca, 'YTickLabel', 1:size(X,1)); %// Add colour bar colorbar; </code></pre> <p>The code that's at the bottom is required because defining the finer grid ultimately increases the size of the image. I need to relabel the axes to go back to the original size.</p> <p>We get this:</p> <p><img src="http://i.stack.imgur.com/UDORw.png" alt="enter image description here"></p> <h1>Small Note</h1> <p>I'm using MATLAB R2014a, and the default colour map is jet. You're using R2014b+ and the default colour map is parula. You won't get the same colour distribution as me, but you will get the smoothness you desire.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31063591/In%20matlab,%20how%20to%20'smooth'%20pixels%20in%202D%20heatmap%20using%20imagesc/31064073">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus