0

I have 3 temperature values for January, one for February and one for march. I want to draw them on one bar graph. I used the overlay method in matlab to draw the 3 values for January. But when I plot the other 2 months, they overlay January. How to enforce February and march values to be side by side to January.

Update: I added the output of running the code below, and changes I want to have

temp_high = [12.5]; 
w1 = 0.5; 
bar(x,temp_high,w1,'FaceColor',[0.2 0.2 0.5])

temp_low = [10.7];
w2 = .25;
hold on
bar(x,temp_low,w2,'FaceColor',[0 0.7 0.7])

temp_very_low = [7.1];
w2 = .1;
hold on
bar(x,temp_very_low,w2,'FaceColor',[0 0 0.7])

ax = gca;
ax.XTick = [1]; 
ax.XTickLabels = {'January'};
ax.XTickLabelRotation = 45;

name={'feb';'march'};

y=[5 ;
 3   ]

bar_handle=bar(y);
set(gca, 'XTickLabel',name, 'XTick',1:numel(name))

ylabel('Temperature (\circF)')
legend({'jan 1-with 1-instance','jan 1-with 2-instance','jan 1-with 3-instance','feb', 'march'},'Location','northwest')

enter image description here

Heba Mohsen
  • 125
  • 6
  • x is the temperature for jan. and I used overlay method so they appear in one bar. The values of y are the temperature values for feb and march. – Heba Mohsen Jun 19 '19 at 04:49

1 Answers1

3

The main issue with your code is in bar(y). The two values in y are implicitly plotted at the x values 1 and 2. What you want is, to plot them at 2 and 3. So, you must explicitly specify these values.

I took the liberty to re-organize your code by collecting all temperature data, widths, and colors in variables. In doing so, all bar plots can be done within a single loop.

Here's the code:

figure(1);
hold on;

% Collect all data.
temp = [1 12.5; 1 10.7; 1 7.1; 2 5; 3 3];
w = [0.5 0.25 0.1 0.5 0.5];
c = [0.2 0.2 0.5; 0 0.7 0.7; 0 0 0.7; 1 0 0; 0 0 1];

% Plot all temperatures within single loop.
for ii = 1:numel(w)
  bar(temp(ii, 1), temp(ii, 2), w(ii), 'FaceColor', c(ii, :));
end

% Decoration.
ticks = [1 2 3];
xlabels = {'January', 'February', 'March'};
set(gca, 'XTick', ticks, 'XTickLabel', xlabels);

ylabel('Temperature (\circF)');
legend({'jan 1-with 1-instance', 'jan 1-with 2-instance', 'jan 1-with 3-instance', 'feb', 'march'}, 'Location','northwest');

hold off;

The output I get, looks like this:

Output

Hope that helps!

HansHirse
  • 18,010
  • 10
  • 38
  • 67
  • Thanks a lot. How to separate January, Feb, March. In other words, how to leave a space after each month? – Heba Mohsen Jun 19 '19 at 06:02
  • @HebaMohsen You talk about the width of the single bars? Therefore, each width in `w` must not exceed `0.5`. Thus, for example, setting the maximum value to `0.45` should be enough. – HansHirse Jun 19 '19 at 06:08
  • I mean how to separate the bars. They are stacked beside each other. I mean leave 5 mm between jan and feb. and feb and march as well – Heba Mohsen Jun 19 '19 at 06:14
  • 1
    @HebaMohsen Yes, that's how I understood your first comment. And, as I said, just set `w = [0.45 0.25 0.1 0.45 0.45];` for example. For further reading, please have a look at the [`width`](https://www.mathworks.com/help/matlab/ref/bar.html#bthf_ju-width) property of the `bar` command. – HansHirse Jun 19 '19 at 06:15
  • one last question, how to make two legends, one for January and the other one for Feb and march – Heba Mohsen Jun 19 '19 at 06:24
  • @HebaMohsen Haven't done that myself before, but here's some [StackOverflow Q&A](https://stackoverflow.com/questions/11253887), which might help. – HansHirse Jun 19 '19 at 06:30