How do I plot lines/points on top of a heatmap?

149 views (last 30 days)
Hi,
I'm trying to plot some scatter plots on top of a heatmap at specific positions (i.e. plot the red dot right in the middle of a color block on the heatmap). And here are two questions I have:
  1. What exactly do the left, bottom, width, height represent? Is there a way I can get some sort of position of e.g., the bottom left corner of the figure and then start from there?
  2. How do different 'layers' of plot work in MATLAB? It seems like in this case there is a layer with the heatmap and another layer with the ax. Also, what is 'axes' here?
data_matrix = 1:12;
data_matrix = reshape(data_matrix,[4,3]);
f = figure(1);
h = heatmap(f,data_matrix);
ax = axes;
position = h.Position; %[left bottom width height]
left = position(1);
bottom = position(2);
width = position(3);
height = position(4);
num_row = 4;
num_col = 3;
plot(ax,left,bottom,'or')
ax.Color = 'none';
ax.XTick = [];
ax.YTick = [];
  1 Comment
Yu Teo
Yu Teo on 28 Jun 2023
By using Pcolor you can just plot the heatmap and overlay the heatmap with other symbols by adding
"hold on" as if it is an axis.
but make sure you add
set(gca, 'YDir','reverse')
to invert the heatmap if you want

Sign in to comment.

Accepted Answer

dpb
dpb on 3 Nov 2022
More About section for heatmap includes the following disclaimer --
Standalone Visualization
A standalone visualization is a chart designed for a special purpose that works independently from other charts. Unlike other charts such as plot and surf, a standalone visualization has a preconfigured axes object built into it, and some customizations are not available. A standalone visualization also has these characteristics:
  • It cannot be combined with other graphics elements, such as lines, patches, or surfaces. Thus, the hold command is not supported.
The upshot is that you probably will not be able to use the heatmap in this manner -- cannot draw a second axes on top of the first at the same positions as is the way these things are normally done and layers only have bearing within the same figure with multiple axes.
The only way I believe you will be able to make such a figure will be to use the primitives of pcolor or surf or similar; the heatmap object is untouchable.

More Answers (1)

Kevin Holly
Kevin Holly on 3 Nov 2022
Edited: Kevin Holly on 3 Nov 2022
1. The left and bottom variables in this code represents the coordinates of the bottom left portion of the heatmap graphic. To access the bottom left coordinate of the figure window in relation to your screen, you can use the figure handle to access the 'Position' subfield.
f = figure;
f.Position
ans = 1×4
671 661 577 433
Note, the coordinates my be in pixels. To know what units you are working with, you can look at the 'Units' subfield within the figure handle as such:
f.Units
ans = 'pixels'
2. Figure is the entire window. HeatmapChart and the Axes are Children of the Figure. They are separate graphics.
% create a matrix by taking a vector array from 1 to 12 and reshaping it
% into 4 rows and 3 columns
num_row = 4;
num_col = 3;
data_matrix = 1:12;
data_matrix = reshape(data_matrix,[4,3]);% could be written as data_matrix = reshape(data_matrix,[num_row,num_col])
% Create heatmap on figure 1
f = figure(1);
h = heatmap(f,data_matrix);
Let's look at the Children of the Figure
f.Children
ans =
HeatmapChart with properties: XData: {3×1 cell} YData: {4×1 cell} ColorData: [4×3 double] Show all properties
Look at the position of the heatmap relative to the figure's dimensions by using the heatmap's handle (in this case denoted by the variable h).
position = h.Position %[left bottom width height]
position = 1×4
0.1300 0.1100 0.7178 0.8150
left = position(1)
left = 0.1300
bottom = position(2)
bottom = 0.1100
width = position(3)
width = 0.7178
height = position(4)
height = 0.8150
Note, these units are not pixels, but are the normalize units ranging from 0 to 1 within the figure window.
h.Units
ans = 'normalized'
axes creates a graphic where you can plot lines, place markers, patches, and other graphic objects. ax in this case is the handle of the axes, which allows you see the properties of the axes and possibly edit them. By default the axes's background color is white. Here the color is made none so that only the children of the axes appears. In this case, the child would be the plotted line, which would be a marker in this case since the line specfication was set to 'or' - meaning o shaped markers that are red. For different line specification options see documentation here.
ax = axes
ax =
Axes with properties: XLim: [0 1] YLim: [0 1] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
plot(ax,left,bottom,'or')
ax.Color = 'none';
ax.XTick = [];
ax.YTick = [];
Note, the figure now has two children. The heatmap and the axes.
f.Children
ans =
2×1 graphics array: Axes HeatmapChart
  1 Comment
dpb
dpb on 3 Nov 2022
@Kevin Holly - " HeatmapChart and the Axes are Children of the Figure. They are separate graphics"
Interesting and useful...I had poked around at the heatmap but given the above quoted information from the MORE section hadn't spent very much effort figuring it was going to be a lost cause anyways.
Had presumed then from that description that the underlying axis object itself was inaccessible.
Another case I think the documentation needs to be updated to reflect reality...and I commend the developers for actually not hiding the axes object entirely as I had thought they had done. Will have to investigate some of the others in more depth as well to see if they were that thoughtful overall.

Sign in to comment.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!