scatter plot with patches
조회 수: 4 (최근 30일)
이전 댓글 표시
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1019790/image.png)
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
Walter Roberson
2022년 6월 2일
which plot is to have log y? I notice that there are negative y for the first plot; you would have problems plotting that with log.
채택된 답변
Voss
2022년 6월 2일
편집: Voss
2022년 6월 2일
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 = 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)
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
Voss
2022년 6월 8일
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.)
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!