How to change Heatmap's Grid (Edge) appereance and behavior?

45 views (last 30 days)
Hi Dear Community,
Today I'm dealing with a heatmap chart to make it look visually beautiful as much as possible. Here below I created some random data with some empty values in it. Later on I realized that the grid color is black and does not change. Also I couldn't find any of these properties: GridAlpha, EdgeAlpha, GridColor, GridLineStyle etc. I was able to control these properties when I use histogram2.
So I thought that if it is a figure and a plot in it, there should be an axis handle for that. I used gca to get axis but it returned me the heatmap object handle itself.
Then I found something interesting and convert the object handle to struct. The Axes field was actually the axis handle. I changed the XAxisLocation to top.
I reached the other properties of the axis but the properties about grid appereance does not work. Eventually, I decided to make grid visible off. But this time it does not look good and hard to understand grid edges.
My goal is make the visualisation plain, with less ink, and understandable.
So my question is how to reduce the GridAlpha on heatmap? Or how can I change its color to white to make it look simple?
% create random data
heatmap_data = randi(20,15,15);
% make some of them empty
heatmap_data(heatmap_data < 15) = NaN;
% create x and y labels
label_names = cellstr(['A':'O']')';
h_fig = figure; % figure handle
h_fig.WindowState = 'maximized';
h_map = heatmap(h_fig,label_names,label_names,heatmap_data,'MissingDataColor',[1 1 1],'ColorbarVisible','off'); % heatmap handle
h_ax = gca; % tried to get axis handle but gives me h_map instead (a heatmap handle)
h_ruler = struct(h_ax); % convert to struct to see undocumented features - meanwhile a warning appears
h_ruler.Axes.XAxisLocation = 'top'; % this actually worked! "h_ruler.Axes" is an Axis Handle.
% but this part does not work:
h_ruler.Axes.GridLineStyle = '--';
h_ruler.Axes.GridAlpha = 0.1;
h_ruler.Axes.GridColor = [0.8510 0.3294 0.1020]; % red color code just for example to show it does not work.
% make grid visible off
h_map.GridVisible = 'off'; % this time heatmap looks bad. It is not easy to understand edges.

Accepted Answer

Yair Altman
Yair Altman on 5 Mar 2020
There are several alternatives:
  1. Update the grid style to a non-solid line, for example:
hHeatmap = struct(h_map).Heatmap;
hHeatmap.GridLineStyle = ':';
2. Update the grid color to something different than the default, which is uint8([38;38;38;255])
hHeatmap = struct(h_map).Heatmap;
hGrid = struct(hHeatmap).Grid;
hGrid.ColorData = uint8([238;238;238;125]);
For a description of the uint8 4x1 ColorData format, see http://undocumentedmatlab.com/articles/plot-line-transparency-and-color-gradient
  2 Comments
Luna
Luna on 6 Mar 2020
Thank you Yair, I was so close to find it! haha :) It definetely works well.

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!