How best to overlay bar graphs from histcounts of different data? (2024)

11 views (last 30 days)

Show older comments

hxen on 9 Jun 2023

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data

Edited: Matt J on 9 Jun 2023

Accepted Answer: Matt J

Open in MATLAB Online

I wish to overlay bar graphs from two different samples that use histcounts. The length of the separate counts are not equal nor neccessarily is their bin sizes. I am using the Freedman-Diaconis bin method to ensure the best bin sizing automatically. Ultiamtely I want to best visualize the different data by color and having all the bars of the same width--and importantly in an interlaced (or side by side) fashion. I am not sure if this is possible and cannot get around doing this. I should add, I am centering the bins between the bin edges for the data and adjusted the bars to have the same width. Here's an example:

figure;

[v1, e1] = histcounts([2 5 3.4 5.3 5 4 8 7],'BinMethod','fd');

de = diff(e1)/2; % difference btwn bins

wcl1 = e1(1:end-1) + de; % bin center location

b1 = bar(wcl1,v1,0.25);

hold on;

[v2, e2] = histcounts([3.4 5.5 6.4 6.9 7],'BinMethod','fd');

de = diff(e2)/2;

wcl2 = e2(1:end-1) + de;

b2 = bar(wcl2,v2,0.25);

hold off

How best to overlay bar graphs from histcounts of different data? (2)

As it turned out the centered bin locations are identical for both data set and this visualization gives the false impression any one bar are is highlighting a breakdown of composing parts. How best to deal with such a case? Can I get the bar function to have the blue and red bars of say at column 5 side by side? In centering between the bin edges, I believe this is the best approach but I am open to any suggestion for best visualizing. Thank you in advance for any help or suggestions.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Matt J on 9 Jun 2023

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data#answer_1253329

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data#answer_1253329

Open in MATLAB Online

A solution with interlacing:

[v1, e1] = histcounts([2 5 3.4 5.3 5 4 8 7],linspace(3,6,4));

[v2, e2] = histcounts([3.4 5.5 6.4 6.9 7],'BinMethod','fd');

fcn=@(z)conv(z,[1,1]/2,'valid');

e1=fcn(e1);

e2=fcn(e2);

E=unique([e1,e2]);

V1=interp1(e1,v1,E);

V2=interp1(e2,v2,E);

bar(E,[V1;V2])

xticks(E)

How best to overlay bar graphs from histcounts of different data? (4)

1 Comment

Show -1 older commentsHide -1 older comments

hxen on 9 Jun 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data#comment_2777264

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data#comment_2777264

Nice! :)

I also found this that I will leave the link in case anyone encounters this and wants to read more about interleaving:

https://www.mathworks.com/matlabcentral/answers/725247-how-do-i-combine-two-bar-graphs

Sign in to comment.

More Answers (2)

Matt J on 9 Jun 2023

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data#answer_1253324

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data#answer_1253324

Edited: Matt J on 9 Jun 2023

Open in MATLAB Online

Maybe make the bars semi-transparent? You can also play with the edge thicknesses to emphasize the area of inclusivity of the bars.

[v1, e1] = histcounts([2 5 3.4 5.3 5 4 8 7],'BinMethod','fd');

[v2, e2] = histcounts([3.4 5.5 6.4 6.9 7],'BinMethod','fd');

fcn=@(z)conv(z,[1,1]/2,'valid');

b1 = bar(fcn(e1),v1,'w','FaceAlpha',0.5,'LineWidth',2,'EdgeColor','r'); hold on;

b2 = bar(fcn(e2),v2,'b','FaceAlpha',0.5,'EdgeColor','none'); hold off;

How best to overlay bar graphs from histcounts of different data? (7)

1 Comment

Show -1 older commentsHide -1 older comments

hxen on 9 Jun 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data#comment_2777259

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data#comment_2777259

Hi. Matt. I ended up doing that for now. But I would be shocked there isn't a work around to do the kind of interleaved plotting between two groups. If you are familiar with the stats program Prism, that is a feature built in but you have to copy and past large data sets, which is prone to copy and past errors. So working to have it as much in my analysis routines before having to use an outside program. Thank you for posting.

Sign in to comment.

hxen on 9 Jun 2023

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data#answer_1253339

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1980864-how-best-to-overlay-bar-graphs-from-histcounts-of-different-data#answer_1253339

Thanks Matt. :)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAnnotations

Find more on Annotations in Help Center and File Exchange

Tags

  • bar graph
  • histcounts
  • overlaid bars

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How best to overlay bar graphs from histcounts of different data? (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How best to overlay bar graphs from histcounts of different data? (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 6507

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.