Creating a Multi Bar Plot in Matlab with Zeros and Proper Legends?

  • Thread starter Thread starter madtraveller
  • Start date Start date
  • Tags Tags
    Matlab Plot
Click For Summary

Discussion Overview

The discussion revolves around creating a multi bar plot in MATLAB using a data frame representing different soil types and depths. Participants are addressing issues related to plotting zero values and configuring legends correctly.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • madtraveller presents a data frame and seeks to create a multi bar graph with 16 columns for soil types and 6 sub-columns for soil depths, noting issues with MATLAB not plotting zero values.
  • Some participants suggest using the basic bar function and modifying the data structure, but madtraveller indicates these suggestions do not meet the requirement of displaying all columns, including those with zero values.
  • One participant proposes adding a small value to the zero entries as a workaround to ensure all columns are plotted.
  • Another participant shares a solution involving transposing the data frame, which resolves the plotting issue but raises a new question about displaying x-axis labels correctly.

Areas of Agreement / Disagreement

Participants express differing views on how to handle zero values in the data for plotting. While some suggest workarounds, there is no consensus on the best approach to achieve the desired plot configuration.

Contextual Notes

Participants have not resolved the issue of displaying x-axis labels correctly after transposing the data. There are also unresolved concerns about the effectiveness of proposed solutions for plotting zero values.

madtraveller
Messages
28
Reaction score
0

Homework Statement



I have a 6 x 16 data frame "y" which stands for 16 different soil types at 6 different depth as followings

Code:
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0.37	0	0	0.50	0	0.03	0.03	0	0	0.02	0.05
0	0	0	0	0	0.26	0	0	0.34	0	0.02	0.02	0	0	0.33	0.03
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0
0	0	0	0	0	0.06	0	0	0	0	0	0	0	0	0.94	0


Homework Equations



I'd like to create a multi bar graph which has 16 columns (soil type) and each column has another 6 sub column(6 soil depths). But it seemed that Matlab didn't like zeros value, thus it didn't plot it and I couldn't create a proper legend for it

The Attempt at a Solution



Code:
figure;
% Legend for x-axis
soilType = {'S', 'LS', 'SL', 'SIL', 'SI', 'L', 'SCL',...
            'SICL', 'CL', 'SC', 'SIC', 'C', 'OM', 'W', 'BR', 'O'};
% Characteristics for legend
soilDepth = {'0-10cm', '0-100cm', '0-150cm', '10-40cm', '40-100cm', '100-150cm'};
bar(y, 'group') 
set(gca, 'XTickLabel', soilType);
xlabel('Soil Type');
ylabel('Areal Fraction')
legend(soilDepth, 'Location', 'Northwest')


Any suggestion will be very appreciated. Thx

madtraveller
 
Physics news on Phys.org
Take a look at this:

http://www.mathworks.com/help/techdoc/ref/bar.html

Assuming that your matrix is called yMat, you can use:

figure;
bar(yMat);

If that doesn't work, take a look at this:

http://compgroups.net/comp.soft-sys...-on-a-Bar-Graph-with-different-x-and-y-values

From what I got from that, you can create the following vectors: X = <1,2,..,16>, Y1, Y2, Y3, Y4, Y5, and Y6 (where each Y is a row). Then you can use:

bar(X,Y1)
hold on;
bar(X,Y2)
hold on;
.
.
.
bar(X,Y6)

and this should superimpose the bar graphs. The first method should work though.
 
Hi gb7nash,

The first suggestion is kind of identical to what I did and it didn't work
The second one didn't help either because what I need is to have 6 rows plotted beside each other with 6 different colors (16 columns x 6 color). In the end I will create a legend indicating 6 different color (soil depth)

Thx anyway

madtraveller
 
Dear all,

I still need help with this. The legend didn't display correctly with the above code and Matlab just plot the columns having non-zeros value instead of plotting all 16 columns

Thx in advanced

madtraveller
 
Maybe you could put a really small value in each column of one row? For instance, putting 0.001 instead of 0 for all the entries in the first row?

>> data(1,:)=0.001

A hack, but maybe just good enough to make your graph for you.
 
Hello MATLABdude,

I've just found out that you don't have to put small values at all. I just transpose my data frame "y" like this and it works nicely

Code:
bar(y', 'group')

However, the xlabel on x-axis still didn't display the way I want. Do you know a way to force all the labels show up on x-axis?

Thx
 

Similar threads

Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
6K