scatter plot with patches

13 views (last 30 days)
Roja Eliza
Roja Eliza on 2 Jun 2022
Commented: Voss on 8 Jun 2022
My data has some concentrations(y axis) ranging from 0-600 mostly and one area values aruond 30,000 at 4 different locations. I ran this code works fine for random matrices but the background gray patches dissapear when I enter my observation data(tried to change the x y values but cann't seem to get how it works). Also as my data on y axis varies so much I want to represent in log scale or anyway it could be clearly visualized.
sites = compose("%s%d",["A" "B" "C" "D"].',1:6).';
sites = sites(:);
sites([4 5 6 10 11 12]) = [];
n_sites_per_group = [3 3 6 6];
elements = ["Cr" "Mn" "Mo" "Ni" "V" "Co" "Zn" "Cu" "Ga" "Cd" "Sn" "zr" "Pb" "Fe"];
n_sites = numel(sites);
n_elements = numel(elements);
data=num; %data ranges from 0 to 30,000 imported from excel
colors = [ ...
0 0 1; ...
0 0.6 0; ...
1 0 0; ...
0.6 0.6 0; ...
0.6 0.6 0.6; ...
0.6 0.6 0; ...
1 0 1;...
1 1 0;...
1 0.6 1;...
1 0.6 0.6;...
0.6 1 0.6;...
1 1 0.6;...
0 0 0;...
0 1 0;];
markers = 'o+*.x_|sd^v><p';
hold on
lines = zeros(1,n_elements);
for ii = 1:n_elements
lines(ii) = line(1:n_sites,data(:,ii), ...
'LineStyle','none', ...
'Marker',markers(ii), ...
'Color',colors(ii,:), ...
'MarkerFaceColor',colors(ii,:), ...
'DisplayName',elements(ii));
end
x_edges = [0 0.5+cumsum(n_sites_per_group(1:end-1)) n_sites+1]
xline(x_edges(2:end-1),'Color',[0.4 0.4 0.4]);
text((x_edges(1:end-1)+x_edges(2:end))/2,2*ones(1,numel(n_sites_per_group)), ...
["AHF" "AHA" "AHR1" "AHR2" ], ...
'VerticalAlignment','top', ...
'HorizontalAlignment','center', ...
'FontWeight','bold', ...
'FontSize',8);
xlim(x_edges([1 end]));
ylim([0 30,000]);
xlabel('Sampling site','FontWeight','bold');
ylabel('Conc-mg/L','FontWeight','bold');
set(gca(), ...
'Layer','top', ...
'Box','on', ...
'XTick',1:n_sites, ...
'XTickLabels',sites, ...
'TickLength',[0.005 0.005]);
y= (5:-.1:0)+[0;1;1;0] ; %from here I am unable to get it
x=repmat([0;0;n_sites+[1;1]],1,6);
c = repmat(linspace(0.5,0.95,6).',1,3);
p = patch( ...
'XData',x, ...
'YData',y, ...
'FaceColor','flat', ...
'FaceVertexCData',c, ...
'EdgeColor','none');
ch = get(gca(),'Children');
set(gca(),'Children',[ch(ch ~= p); p]);
legend(lines,'Location','SouthEast','NumColumns',2);
  2 Comments
Roja Eliza
Roja Eliza on 2 Jun 2022
I wanted to have log scale on y axis. the answer below given by Voss solved it. Thanks anyway

Sign in to comment.

Accepted Answer

Voss
Voss on 2 Jun 2022
Edited: Voss on 2 Jun 2022
One difference in the grey patches between the previous plot (here) and the one you're making now, is that before, the patches were all the same height (1), and now they vary in height, so you have to construct their YData a little differently. See below.
sites = compose("%s%d",["A" "B" "C" "D"].',1:6).';
sites = sites(:);
sites([4 5 6 10 11 12]) = [];
n_sites_per_group = [3 3 6 6];
elements = ["Cr" "Mn" "Mo" "Ni" "V" "Co" "Zn" "Cu" "Ga" "Cd" "Sn" "zr" "Pb" "Fe"];
n_sites = numel(sites);
n_elements = numel(elements);
% data=num; %data ranges from 0 to 30,000 imported from excel
data = 30*rand(n_sites,n_elements);
colors = [ ...
0 0 1; ...
0 0.6 0; ...
1 0 0; ...
0.6 0.6 0; ...
0.6 0.6 0.6; ...
0.6 0.6 0; ...
1 0 1;...
1 1 0;...
1 0.6 1;...
1 0.6 0.6;...
0.6 1 0.6;...
1 1 0.6;...
0 0 0;...
0 1 0;];
markers = 'o+*.x_|sd^v><p';
hold on
lines = zeros(1,n_elements);
for ii = 1:n_elements
lines(ii) = line(1:n_sites,data(:,ii), ...
'LineStyle','none', ...
'Marker',markers(ii), ...
'Color',colors(ii,:), ...
'MarkerFaceColor',colors(ii,:), ...
'DisplayName',elements(ii));
end
x_edges = [0 0.5+cumsum(n_sites_per_group(1:end-1)) n_sites+1];
xline(x_edges(2:end-1),'Color',[0.4 0.4 0.4]);
text((x_edges(1:end-1)+x_edges(2:end))/2,60*ones(1,numel(n_sites_per_group)), ...
["AHF" "AHA" "AHR1" "AHR2" ], ...
'VerticalAlignment','top', ...
'HorizontalAlignment','center', ...
'FontWeight','bold', ...
'FontSize',8);
xlim(x_edges([1 end]));
% ylim([0 30,000]);
ylim([0.075 60]);
xlabel('Sampling site','FontWeight','bold');
ylabel('Conc-mg/L','FontWeight','bold');
set(gca(), ...
'Layer','top', ...
'Box','on', ...
'XTick',1:n_sites, ...
'XTickLabels',sites, ...
'TickLength',[0.005 0.005], ...
'YScale','log');
% y = (5:-.1:0)+[0;1;1;0] ; %from here I am unable to get it
% x = repmat([0;0;n_sites+[1;1]],1,6);
% c = repmat(linspace(0.5,0.95,6).',1,3);
y_levels = [60 50 25 10 5 3 1] % these will be the edges of the grey patches
y_levels = 1×7
60 50 25 10 5 3 1
y = y_levels((1:end-1)+[0;1;1;0]) % construct y by indexing into y_levels (rather than specifying integers 5:-1:0 and adding 1, as it was before)
y = 4×6
60 50 25 10 5 3 50 25 10 5 3 1 50 25 10 5 3 1 60 50 25 10 5 3
x = repmat([0;0;n_sites+[1;1]],1,size(y,2)); % also make the size of x and c depend on the size of y.
c = repmat(linspace(0.5,0.95,size(y,2)).',1,3); % note that these use size(y,2) now, instead of hard-coded 6
p = patch( ...
'XData',x, ...
'YData',y, ...
'FaceColor','flat', ...
'FaceVertexCData',c, ...
'EdgeColor','none');
ch = get(gca(),'Children');
set(gca(),'Children',[ch(ch ~= p); p]);
legend(lines,'Location','SouthEast','NumColumns',2);
  4 Comments
Voss
Voss on 8 Jun 2022
I don't think there's any built-in MATLAB function to do that, but you might try this File Exchange submission:
(I haven't used it.)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!