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

조회 수: 46 (최근 30일)
Luna
Luna 2020년 3월 5일
편집: shuyang zhang 2021년 5월 31일
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.

채택된 답변

Yair Altman
Yair Altman 2020년 3월 5일
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
Luna
Luna 2020년 3월 6일
Thank you Yair, I was so close to find it! haha :) It definetely works well.
shuyang zhang
shuyang zhang 2021년 5월 31일
편집: shuyang zhang 2021년 5월 31일
Thank you Yair, Very USEFUL!!!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by