How to copy paste a bar graph on an existing bar plot and have data merged with bars' width resizing automatically?

4 views (last 30 days)
Hi all,
I have two matlab (2016b) bar plots.
In one graph I have, say, three bars for each x-value, e.g.:
figure;
bar([2 4 6 8 10],[1 2 3 4 5;2 4 9 1 6;3 1 2 7 6].','BarWidth',1)
in the second graph I have another bar graph, at the very same values of x:
figure;
bar([2 4 6 8 10],[7 8 9 8 7].','BarWidth',1)
Now, what I would like to do is to copy-paste the bar from the second plot into the first plot. And this is the easy part I know how to do ;-)
But in addition to this, I'd also like that matlab considers the original bars+the copy-pasted bar as a single group of bars, in such a way that the bars do not overlap, but they are plotted close to each other with automatically resized width (to be controlled through the "BarWidth" property).
Basically, I'd like the final result to be like:
figure;
bar([2 4 6 8 10],[1 2 3 4 5;2 4 9 1 6;3 1 2 7 6;7 8 9 8 7].','BarWidth',1)
I tried to find a programmatic way to do this following the manual copy-paste, but it is unclear to me how matlab "links" the different bar plots in order to automatically resize the bar widths. In fact, each set of bars appears as a separate object, and I cannot understand where matlab stores information regarding which bars matlab has to manage as a "group" when resizing the widths through the "BarWidth" property.
Do you have an idea how to do this, or an hint of the bar object properties that I should look at to achieve the desired behaviour?
Thank you, Gabriele

Accepted Answer

Meeshawn Marathe
Meeshawn Marathe on 28 Jul 2017
Hi,
Adding the second bar plot data to the existing bar plot using a "hold on" function will overlap with the existing one and currently cannot be grouped in the existing plot.
The best way to append and group the second bar plot would be the command suggested by you:
bar([2 4 6 8 10],[1 2 3 4 5;2 4 9 1 6;3 1 2 7 6;7 8 9 8 7].','BarWidth',1)
  1 Comment
Gabriele
Gabriele on 28 Jul 2017
Dear Meeshawn Marathe, thank you for your reply.
Let me then suggest, for future relases, to allow the possibility of "merging"/"appending" bars plots. I think it would be very useful in case you have bar plots in different figures and you want to quickly create comparisons graphs.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!