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 Comments

It is possible to make a bar plot with specific colors for specific values and text as well.
n=29.9;
figure
hold on
Y_SBT=0:0.01:n;
bar (SBT, Y_SBT)
grid on
when I run my above code it gives me the following error. SBT is a one colomn matrix with 2991 rows.
Error using bar
XData values must be unique.
Error in Liq1 (line 111)
bar (SBT, Y_SBT)
first I have to run this and then change the colors.
@Mohammad Sadegh Nasirianfar Use barh instead of bar.
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?

Sign in to comment.

 Accepted Answer

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 Comments

fantastic. don't know how to appreciate your response. Thank you, Thank you, Thank you.
You're welcome!

Sign in to comment.

More Answers (0)

Products

Release

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!