Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:08:00+00:00 2026-06-11T20:08:00+00:00

I’m trying to make a stacked bar plot that should show how different types

  • 0

I’m trying to make a stacked bar plot that should show how different types of rock are varying with depth. I have generated some fictive lithology series based on some real data and want to present them in a readable way. So now I have this matrix A:

A =

2.0000   65.0000
1.0000   19.5000
2.0000    0.5000
4.0000    1.5000
2.0000   58.0000
4.0000    2.0000
2.0000   22.5000
3.0000    7.0000
2.0000   14.5000
3.0000   12.5000
4.0000    2.5000
2.0000   31.5000
1.0000   20.0000
2.0000   20.0000
1.0000   15.5000
2.0000   66.0000
4.0000    0.5000
2.0000    2.5000
3.0000    8.0000
2.0000   61.0000
1.0000   17.5000
2.0000    8.0000
5.0000   19.5000
3.0000   24.5000

where the first column represents the different rock types and the second column the thickness (in meters) of each lithology layer. And now I want to plot this looking like a core-data log. So each rock type, from 1-5, should have one specific color, and the thickness of each colored bar should represent the thickness of that rock type. How can I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:08:01+00:00Added an answer on June 11, 2026 at 8:08 pm

    Aggregate per type

    You can find the total thickness of each rock type by using accumarray:

    total_type_thickness = accumarray(A(:,1),A(:,2));
    

    It sums up all values of the second column with equal number in the first column. So for your example data, this returns:

    total_type_thickness =
    
       72.5000
      349.5000
       52.0000
        6.5000
       19.5000
    

    The difficult thing is displaying it as a single stacked bar, you can try using this workaround:

    http://www.mathworks.com/matlabcentral/newsreader/view_thread/57304

    which leaves you with an empty column, which you can hide by setting the x limits:

    bar([total_type_thickness'; zeros(size(total_type_thickness'))],'stacked');
    xlim([0.25 1.75])
    % add a legend with 'Rock Type <ii>'
    legend(arrayfun(@(ii) sprintf('Rock type %d',ii), 1:length(total_type_thickness), 'uni',false));
    

    enter image description here

    For now, I haven’t found a better alternative, as matlab won’t stack the bars if you input vector data, it plots the bars separately in that case..

    Display all values, same color per type

    For plotting all the data (all layers), you can use the same approach, but now set the color data manually with colormap:

    N = size(A,1); % number of layers
    M = max(A(:,1)); % number of different rock types
    
    bar([A(:,2)' ; NaN(1,N)],'stacked','facecolor','flat');
    xlim([0.25 1.75])
    
    cc = jet(M); % create colormap with N different colors
    colormap(cc(A(:,1),:)); % pick for each layer, the correct color and use it as a colormap
    

    For easier adding a legend, I’ll add M dummy values to the original data:

    bar([NaN(1,M)  A(:,2)' ; NaN(1,N+M)],'stacked','facecolor','flat');
    xlim([0.25 1.75])
    
    cc = jet(M); % create colormap with N different colors
    colormap(cc([(1:M)' ;A(:,1)],:)); % pick for each layer, the correct color and use it as a colormap
    

    Now the first M elements in the legend will correspond to Rock type 1,2,.. M:

     legend(arrayfun(@(ii) sprintf('Rock type %d',ii), 1:length(total_type_thickness), 'uni',false));
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.