I want to display a bar plot.

2 次查看(过去 30 天)
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 个评论
Mohammad Sadegh Nasirianfar
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?

请先登录,再进行评论。

采纳的回答

Voss
Voss 2022-5-8
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 个评论
Mohammad Sadegh Nasirianfar
fantastic. don't know how to appreciate your response. Thank you, Thank you, Thank you.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by