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 个评论
采纳的回答
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))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!