Scatter plot with strings on x axis
조회 수: 13 (최근 30일)
이전 댓글 표시
I want to plot a scatter plot with sampling sites on x axis like S1, s2 names and on y axis I want to plot concentration of various metals. Also I want to divide the plot into sections as in the figure. If I am using xline command then it is plotting a line on the sampling sites but I want the dividing line to be between s2and S3.for example here in the picture the dividing line is between j5 and ju1 Can anyone help?
댓글 수: 0
채택된 답변
Voss
2022년 5월 23일
편집: Voss
2022년 5월 23일
sites = compose("%s%d",["J" "Ju" "P" "R" "S" "W"].',1:5).';
sites = sites(:);
sites([12 14]) = [];
n_sites_per_group = [5 5 3 5 5 5];
elements = ["Cr" "Ni" "Cu" "Zn" "Cd" "Pb"];
n_sites = numel(sites);
n_elements = numel(elements);
data = 1.25*randn(n_sites,n_elements)-2; % random data
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];
markers = 'osd^+*';
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,5.8*ones(1,numel(n_sites_per_group)), ...
["Jezewo" "Jutrosin" "Pakoslaw" "Rydzyna" "Sroda" "Wrzesnia"], ...
'VerticalAlignment','top', ...
'HorizontalAlignment','center', ...
'FontWeight','bold', ...
'FontSize',8);
xlim(x_edges([1 end]));
ylim([-8 6]);
xlabel('Sampling site','FontWeight','bold');
ylabel('I_{geo}','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];
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);
댓글 수: 4
Voss
2022년 6월 2일
@Roja Eliza Please start a new question explaining the situation, and include your code.
추가 답변 (1개)
dpb
2022년 5월 23일
<scatter> can take a categorical variable as an x variable in which case you can get the text labels automagically -- however, the x-axis then will be categorical and have only integral values of those categories that can be plotted on it.
Your options are to either use a numeric category number for x and the set the tick labels as wanted with <xticklabels> which lets you write any string desired at any tick.
You can then draw the vertical lines at any x value you wish between the integral tick values.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!