Set individual bar color, with specifying y axis range and show tick as well.

2 visualizaciones (últimos 30 días)
Hi All,
I am trying to set individual bar color but I can not set it to be accurately shown. Also, I would like to specify range of y axis like (from 0 to 10 every 2 increment).
I am using MAtlab version 2016a:
tensile=[61 216 150 194 206 164];
Elongation=[439 4.22 4.23 8.97 8.42 4.98];
Elastic_mod=[0.53 7.73 3.61 3.12 3.75 4.37];
figure1=figure;
subplot(2,2,1)
bar(tensile)
%bar(1:numel(tensile),diag(tensile)) % this code set different colors but not accurate
title('Tensile Strength of different materials')
x_axisname={'Nylon','CFConc','KFConc','GFConc','GFISO','KFISO'};
set(gca,'xticklabel',x_axisname)
ylabel('MPa')
set(gca,'FontSize',17)
%% how can I change the title size font ?
suptitle('Comparison between different fiber pattern (CONCENTRIC & ISOTROPIC)')
set(gca,'FontSize',21) % not working

Respuestas (1)

dpb
dpb el 4 de Jul. de 2019
Editada: dpb el 5 de Jul. de 2019
To set individual bar colors--
hBar=bar(tensile); % do the bar plot, save the handle
hBar.FaceColor='flat'; % set to 'flat' so will use CData array for colors
hBar.CData(2,:)=[1 0 0]; % set 2nd bar to red
follow the example for other bars w/ desired color triplets. This is an example in the documentation
You can set y axis limits however you wish with ylim and yticks but with data from 0-200 a tick spacing of 2 will have 100 ticks and will be essentially a solid blob.
Your x tick labels are too long to fit as well; you could try something like--
hAx=gca; % get handle to the axes object
xticklabels(x_axisname)
hAx.XTickLabelRotation=30;
hAx.FontSize=8;
is at least legible on default figure/subplot size; salt to suit...
I don't have suptitle and a search of ML documentation doesn't find it; there apparently is a FEX submittal by this name. To make changes to its default properties (it appears to be just a text() object underneath), also save the handle and modify it...
hSup=suptitle('your text');
hSup.FontSize=17;
If this function is well written, you should be able to pass parameters to it when called--but I didn't investigate.
Upshot is, "read the documentation" for all the functions you're using including looking at examples; most all if not all will be explained therein.
ADDENDUM
For earlier releases, if y is a vector, bar(y) is a single object but if use an array, while it creates a separate bar object for each column in y, it arbitrarily groups by the number of rows in y; excepting a 1-row column array is still treated as a vector. The upshot being there is no way to create more than one object in a single call that isn't grouped so you can't change colors on anything except all columns at once.
Two ways to go at it; one is fairly straightforward but seems klunky--you just keeping adding each bar one at a time...
hBar=bar(1,tensile(1)); % first bar
hold on % get ready to add rest
for i=2:numel(tensile) % and do so
hBar(i)=bar(i,tensile(i));
end
xticks(1:numel(tensile))
You can now set properties for each bar hBar or you could define the color for each bar at the time it is created as each now is independent of each other so setting all to the same color is ok as "all" is just one each time bar() is called.
The second alternative uses a similar ploy as you attempted via diag(), but instead use the grouping behavior somewhat differently via
hBar=bar([tensile;nan(5,6)]); % grouped bar; only one visible
xlim([0.55 1.45]) % set axis limits to only view the first group
This leaves array of six bar objects can color as desired; unfortunately, the xtick values are 1:6 and 1 is at the center of the first group; NOT tick marks at each bar.
This can be fixed, but one has to dig into undocumented properties to retrieve the coordinates of the bars from which to calculate the actual tick locations in terms of bar coordinates.
xloc=cell2mat(arrayfun(@(h) h.Face.VertexData,hBar,'uni',0); % bar coordinate data
xticks(mean(reshape(xloc(1,1:2:end),2,[]))) % xtick desired locations
xticklabels(x_axisname)
...
Clever, but rather convoluted...
  4 comentarios
Ali Tawfik
Ali Tawfik el 5 de Jul. de 2019
Hi Again,
Thanks for your udpated comments, however, still can not set an individual color for each bar ?So I do not know what is the difference between the new command lines, and the one, I am already working
Also, the ticks not working , error apperead "undefined function"
Thanks,
dpb
dpb el 5 de Jul. de 2019
Editada: dpb el 5 de Jul. de 2019
Can't see your terminal from here...show the actual code used. I don't have R2016a but above was done w/ R2016b. Going clear back to R2014b, only other release installed here, the same idea works except xticks had not yet been introduced...
The difference is twofold--first and most obvious, uses old bar object properties and FaceColor instead of new CData. Secondly, with the latter to create multiple bar objects, each bar is plotted independently of the rest so there are actually six bar plots on top of each other instead of just one. With the earlier release, as explained, it's the only way to create separately-addressable bars except by grouping which is not wanted here.
tensile=[61 216 150 194 206 164];
hBar=bar(tensile(1));
hold on
for i=2:numel(tensile),hBar(i)=bar(i,tensile(i));end
hAx=gca;
hAx.XTick=1:6;
hBar(2).FaceColor='r';
hBar(3).FaceColor=[0 0.5 0.5];
x_axisname={'Nylon','CFConc','KFConc','GFConc','GFISO','KFISO'};
hAx.XTickLabel=x_axisname;
produced
w/ R2014b which shows creating tick locations and labelling them and changing FaceColor with both a named color mnemonic and the RGB triplet for a specific bar.

Iniciar sesión para comentar.

Categorías

Más información sobre Line 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