Borrar filtros
Borrar filtros

Scatter plot with strings on x axis

13 visualizaciones (últimos 30 días)
Roja Eliza
Roja Eliza el 23 de Mayo de 2022
Comentada: Voss el 2 de Jun. de 2022
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?

Respuesta aceptada

Voss
Voss el 23 de Mayo de 2022
Editada: Voss el 23 de Mayo de 2022
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]
x_edges = 1×7
0 5.5000 10.5000 13.5000 18.5000 23.5000 29.0000
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 comentarios
Roja Eliza
Roja Eliza el 2 de Jun. de 2022
Editada: Roja Eliza el 2 de Jun. de 2022
I have 14 elements and my y values range between 1 to 30,000 (like 1.6, 290,13000 these kind). I cann't seem to get the code right. whenever I change the value the patch dissapers. I want to set the Y values in logarithmically increasing value
Voss
Voss el 2 de Jun. de 2022
@Roja Eliza Please start a new question explaining the situation, and include your code.

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 23 de Mayo de 2022
<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.

Categorías

Más información sobre Scatter Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by