Trying to manually set rgb values of cells equal to 0 on contourf figure

조회 수: 2 (최근 30일)
Christian
Christian 2019년 6월 26일
답변: Austin M. Weber 2024년 2월 8일
I am attempting to manually set the rgb values on the displayed heat map/2D graph created by contourf, taking data from my input array (a 33477x13 array) to a dark blue if the cell value is equal to 0. How would I do this?
Here is the code:
function [SMPS_data]=Contourf(A)
i=1;
while i<3
start_date = {'5-June-19 0:00','26-May-19 0:01'};
t1 = datetime(2019,5,26,0,0,0);
t2 = datetime(2019,6,4,0,0,0);
dates=t1:minutes(10):t2;
duration_min = numel(dates)*10;
SMPS_timestep = [0:10:duration_min];
colormap(jet)
SMPS_data=[A];
x=zeros(1,length(SMPS_data));
for j=1:length(SMPS_data)
x(1,j) = j/6;
end
y = [11.5; 15.4; 20.5; 27.4; 36.5; 48.7; 64.9; 86.6; 115.5; 154; 205.4; 273.8; 365.2];
z_o=zeros(1,numel(x));
z_r= SMPS_data(1:end,2:end).';
z = [z_o;z_r];
[X,Y] = meshgrid(x,y);
v=logspace(-1,15,500);
[X,Y] = meshgrid(x, y);
figure
[C,h] = contourf(X,Y,griddata(x,y,z,X,Y,'natural'),v);
C(C==0)
set(gca,'yscale','log')
ylim([-1 250]);
str=sprintf('Time (h), t(0) = %s', start_date{2});
xlabel(str)
ylabel('Particle diameter (\mum)')
set(h,'LineStyle','none');
c =colorbar;
caxis([0 2500])
hl = ylabel(c, 'dN/dlogDp #/cm^{-3}');
set(hl, 'Rotation',90);
set(c,'FontSize',12,'LineWidth',1.5)
set(gca,'FontSize',12,'LineWidth',1.5)
set(gca,'TickDir','out')
set(gca,'XTickMode','auto')
%set(gca, 'XTick',[0 6 12 18 24 30 36 42 48 54])
set(gca, 'XTickLabel',{'5/26','5/27','5/28','5/29','5/30','5/31','6/1','6/2','6/3','6/4'})
set(gca, 'YTick',[10 30 50 100 150 200])
set(gca, 'YTickLabel',{'10','30','50','100','150','200'})
box off
i=i+1;
end
end

답변 (1개)

Austin M. Weber
Austin M. Weber 2024년 2월 8일
I don't know if you are still active, but there is an easy way to do this, as long as your data does not already contain any NaN values.
For heatmaps, you can use standardizeMissing to convert all zeros into missing values, then manually set the color of the missing values:
% Setting random number generation seed so that these results can be reproduced
rng(1)
% Creating data and standardizing all zeros to missing
heatmapdata = round(rand(10,10).*10);
heatmapdata = standardizeMissing(heatmapdata,0);
% Create figure
figure(1)
hm = heatmap(heatmapdata);
colormap("spring") % Spring does not have any blues, so it makes it easy to locate the zeros
% Color the "zeros" (which are now missing) dark blue
darkblue = '#00035b';
hm.MissingDataColor = darkblue;
hm.MissingDataLabel = 'Data = 0';
A very similar process can be done for making a filled contour map:
% Create some data
contourdata = round(rand(25,25).*10);
contourdata = standardizeMissing(contourdata,0);
% Plot the data
figure(2)
cf = contourf(contourdata,'EdgeColor','none');
colormap("spring")
colorbar
% Color the "zeros" (which are now missing) to darkblue by changing the
% background color of the axes
set(gca,'Color',darkblue)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by