Patch plot z-values colorbar not correct

6 views (last 30 days)
Hello,
I have a 'patch' plot with x, y, and z-values. A 2x2 sublot is desired with a single colorbar on the right. The values of the colorbar should be from 0 to 1. However, the values in the plot are an order of magnitude off. My largest value in the upper left plot, for example, is somewhere around 0.08. But, the figure is plotting the value as 0.8.
I've attached my plotting script (below) and the variables needed (attached).
Any ideas?
Thanks.
fig = figure;
subplot(2, 2, 1)
patch(X, Y, norm_DSVD_sum2019_JER,'HandleVisibility','off');
title('Summer 2019, Las Cruces, NM', 'FontSize', 12)
xlim([0 8])
ylim([0 11])
xticks([0 1.25 2.5 5.0 8.0 10.0 20.0 26.0])
yticks([0 1.0 2.0 4.0 8.0 11.0 16.0 22.4])
grid off
hold on
p1 = plot(X, vD_AfGK, 'r', 'LineWidth', 2);
p2 = plot(X, vD_AU, 'r--', 'Linewidth', 2, 'LineStyle', '--');
p3 = plot(X, vD_VD, 'r', 'Linewidth', 2, 'LineStyle', ':');
subplot(2, 2, 2)
patch(X, Y, norm_DSVD_win2019_JER,'HandleVisibility','off');
title('Winter 2019, Las Cruces, NM', 'FontSize', 12)
xlim([0 8])
ylim([0 11])
xticks([0 1.25 2.5 5.0 8.0 10.0 20.0 26.0])
yticks([0 1.0 2.0 4.0 8.0 11.0 16.0 22.4])
grid off
hold on
p1 = plot(X, vD_AfGK, 'r', 'LineWidth', 2);
p2 = plot(X, vD_AU, 'r--', 'Linewidth', 2, 'LineStyle', '--');
p3 = plot(X, vD_VD, 'r', 'Linewidth', 2, 'LineStyle', ':');
subplot(2, 2, 3)
patch(X, Y, norm_DSVD_sum2019_SFL,'HandleVisibility','off');
title('Summer 2019, Mesa, AZ', 'FontSize', 12)
xlim([0 8])
ylim([0 11])
xticks([0 1.25 2.5 5.0 8.0 10.0 20.0 26.0])
yticks([0 1.0 2.0 4.0 8.0 11.0 16.0 22.4])
grid off
hold on
p1 = plot(X, vD_AfGK, 'r', 'LineWidth', 2);
p2 = plot(X, vD_AU, 'r--', 'Linewidth', 2, 'LineStyle', '--');
p3 = plot(X, vD_VD, 'r', 'Linewidth', 2, 'LineStyle', ':');
subplot(2, 2, 4)
patch(X, Y, norm_DSVD_win2019_SFL,'HandleVisibility','off');
title('Winter 2019, Mesa, AZ', 'FontSize', 12)
xlim([0 8])
ylim([0 11])
xticks([0 1.25 2.5 5.0 8.0 10.0 20.0 26.0])
yticks([0 1.0 2.0 4.0 8.0 11.0 16.0 22.4])
grid off
hold on
p1 = plot(X, vD_AfGK, 'r', 'LineWidth', 2);
p2 = plot(X, vD_AU, 'r--', 'Linewidth', 2, 'LineStyle', '--');
p3 = plot(X, vD_VD, 'r', 'Linewidth', 2, 'LineStyle', ':');
lgd = legend([p1(1) p2(1) p3(1)],'Atlas et. al., 1973', 'Atlas and Ulbrich, 1977', 'van Dijk et. al., 2002', 'Position', [.72 .12 .14 .1]);
lgd.FontSize = 6.5;
lgd.Title.String = 'Droplet Size/Velocity Relationships';
sgt = sgtitle('Drop Size and Velocity Distributions');
sgt.FontSize = 20;
ax = axes(fig);
yyaxis(ax, 'left');
han = gca;
han.Visible = 'off';
set(gca, 'ycolor', 'k')
han.XLabel.Visible = 'on';
han.YLabel.Visible = 'on';
xlabel('Raindrop Size [mm]')
ylabel('Raindrop Speed [m/s]')
yyaxis(ax, 'right');
c = colorbar('Position', [0.9214, 0.0935, 0.0165, 0.8147]);
c.Label.String = 'Raindrop Count';
c.Label.FontSize = 12;
colormap('jet')
han.YLabel.Visible = 'on';

Accepted Answer

Adam Danz
Adam Danz on 10 Aug 2020
Edited: Adam Danz on 10 Aug 2020
There are several issues to cover.
1) If you want to use 1 colorbar to represent all subplots, all subplots must have the same scale. That can be achieved using caxis(limits). However, the comment below is not correct.
"The values of the colorbar should be from 0 to 1. However, the values in the plot are an order of magnitude off."
The values that define the color in your subplots are all between ~0 and ~0.534. If you were expecting something different, the data you shared is incorrect.
After each patch() command, you need to set the caxis values to the span of the entire color range using
% These two lines only needs computed once, before plotting is done.
allColorData = [norm_DSVD_sum2019_JER, norm_DSVD_sum2019_SFL,norm_DSVD_win2019_JER,norm_DSVD_win2019_SFL];
colorRange = [min(allColorData), max(allColorData)];
% Repleat this line for each subplot
caxis(colorRange)
You'll notice that the colors now differ. Some plots will not show colors outside of a range of blues. If this is not what you want. you cannot have 1 colorbar for all subplots.
2) The extra invisible axes you're adding to control the colorbar is inefficient and prone to error. It's better to add the colorbar to any of the subplots and specify its position. But before studying this step, see the section below "Use tiledlayout instead of subplot"
To add the colorbar, put this after your last subplot
cb = colorbar('Position', [0.9214, 0.0935, 0.0165, 0.8147]);
You may want to move the subplots a bit to make room for the colorbar.
% sp is a 1x4 vector of subplot handles created by
% sp(1) = subplot(___);
% sp(2) = subplot(___);
% etc....
% Move subplots to the right by 0.05 normalized units
arrayfun(@(i)set(sp(i), 'Position', [sp(i).Position(1)-0.05, sp(i).Position(2:end)]), 1:numel(sp));
The bottom and right super-labels can be added easily using text objects.
Use tiledlayout instead of subplot
tiledlayout was introduced in r2019b and allows you to set x and y labels for all subplots. To convert your code to tiled layout,
1) After creating the figure define the layout
tlay = tiledlayout(2,2);
2) change all of your subplot commands to
sp(1) = nexttile(); % replace 1 with whatever subplot you're creating
3) There are some restrictions with the colorbar. It can only be next to 1 subplot. So, remove the 'position' property.
c = colorbar();
If the position restriction is a big problem, you'll have to add an invisible axis just for the colorbar.
ax = axes('position', [.9,.1,.08,0.75]);
caxis(colorRange)
c = colorbar('Location','East');
c.Position(3) = .015
axis(ax, 'off')
4) Then define the x and y labels
xlabel(tlay, 'Raindrop Size [mm]')
ylabel(tlay, 'Raindrop Speed [m/s]')
and replace the sgtitle command with
title(tlay, 'Normalized Drop Size and Velocity Distributions', 'FontSize', 20)
Notice that colors beyond blue are really only seen in Mesa AZ in the summer. It makes sense that rainfall in the summer will have a higher drop count than in the winter. However, it might be surprising that rainfall isn't similar between Las Cruces and Mesa in the summer on bestplaces.net (I have no idea how valid those data are).
  28 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Line 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!