I want to display a bar plot.

2 visualizaciones (últimos 30 días)
Mohammad Sadegh Nasirianfar
Mohammad Sadegh Nasirianfar el 8 de Mayo de 2022
Comentada: Voss el 9 de Mayo de 2022
I want to display a bar plot like attached file i which for each values (2,3,4,5,6,7) display the bar plot with its specific color. also I want to write a text for each of this values as figure. is it possible?
  5 comentarios
Voss
Voss el 8 de Mayo de 2022
@Mohammad Sadegh Nasirianfar Use barh instead of bar.
Mohammad Sadegh Nasirianfar
Mohammad Sadegh Nasirianfar el 8 de Mayo de 2022
I used barh for make it horizontaly but about the values for different color, I do not know anything and tried a lot. do I have use 'if' or 'for' or other commands?

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 8 de Mayo de 2022
Something like this would work, for the bars:
data = randi([3 6],120,1);
colors = [ ...
61 99 89; ...
66 145 128; ...
135 189 166; ...
191 163 97]/255;
barh((1:120)/4,data, ...
'BarWidth',1, ...
'FaceColor','flat', ...
'CData',colors(data-2,:), ...
'EdgeColor','none')
set(gca(),'YDir','reverse','XLim',[0 18])
Here, since each element of data is an integer between 3 and 6 (inclusive), and since each bar is colored according to the value of the corresponding element in data, I use data to index into the set of colors colors. Specifically, when data is 3, the corresponding color is the 1st row of colors, when data is 4, the color is the 2nd row of colors, and so on, so colors(data-2,:) is the colors of all the bars.
For the text labels, I assume that a data value of 2 corresponds to 'Clay', 3 is 'Clay & silty clay', and so on, so that determining the label for each data point can be done with the same indexing as for the colors. It's not clear how to determine which bars get text labels, so here I'm making a label for every 6th bar. I imagine the final result would require some fine-tuning by hand to choose which bars get labels.
labels = { ...
'Clay' ...
'Clay & silty clay' ...
'Silty sand & sandy silt' ...
'Sand & silty sand'};
text(9*ones(1,20),(1:6:120)/4,labels(data(1:6:end)-2))
  2 comentarios
Mohammad Sadegh Nasirianfar
Mohammad Sadegh Nasirianfar el 8 de Mayo de 2022
fantastic. don't know how to appreciate your response. Thank you, Thank you, Thank you.
Voss
Voss el 9 de Mayo de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by