Reputation:
I'm trying to create a bar graph, and set every bar a different style (lines, dots, circles and ext...).
In this example:
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
x = [10; 20; 50; 90];
bar(x,y);
All 3 bars have the same style.
How can I change it and set 3 differents styles for the 3 bars ?
Upvotes: 0
Views: 402
Reputation: 2132
I am adding some style changes to what @Luis given
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
x = [10; 20; 50; 90];
h = bar(x,y);
set(h(1),'FaceColor', 'w','LineWidth', 1, 'LineStyle',':');
set(h(2),'FaceColor', 'w','LineWidth', 1, 'LineStyle','--');
set(h(3),'FaceColor', 'w','LineWidth', 1, 'LineStyle','-.');
'FaceColor', 'w'
-- Makes the color of bar white
'LineWidth', 1
-- Width of the border line of bar
'LineStyle',':'
-- Dotted line
'LineStyle','--'
-- Dashed line
'LineStyle','-.'
-- Dash-dot line
For different style for each bar set
figure
hold on;
y = [2 2 3; 0 0 0; 0 0 0;0 0 0 ];
x = [10; 20; 50; 90];
z=bar(x,y);
ylim([0 15]);
set(z,'FaceColor', 'w','LineWidth', 1, 'LineStyle',':');
y = [0 0 0; 2 5 6; 0 0 0;0 0 0 ];
x = [10; 20; 50; 90];
z1=bar(x,y);
set(z1,'FaceColor', 'w','LineWidth', 1, 'LineStyle','-.');
y = [0 0 0;0 0 0; 2 8 9; 0 0 0];
x = [10; 20; 50; 90];
z2=bar(x,y);
set(z2,'FaceColor', 'w','LineWidth', 1, 'LineStyle','-');
y = [0 0 0;0 0 0; 0 0 0; 2 11 12];
x = [10; 20; 50; 90];
z4=bar(x,y);
set(z4,'FaceColor', 'w','LineWidth', 1, 'LineStyle','--');
hold off;
Upvotes: 0
Reputation: 112679
Use a handle output when calling bar
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
x = [10; 20; 50; 90];
h = bar(x,y);
That gives an array h
of bar
objects (of length 3
in your example), and you can set their aspect independently. For example,
set(h(1), 'EdgeColor', 'r');
set(h(2), 'EdgeColor', 'g');
set(h(3), 'EdgeColor', 'b');
gives the following graph in R2015b (the aspect will vary in other versions).
Other properties you can change are 'BarWidth'
, 'LineStyle'
, etc. To see the list type get(h(1))
.
Upvotes: 1