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
AI Thread Summary
The discussion centers on creating a multi bar plot in MATLAB using a data frame representing different soil types and depths. The user faced issues with MATLAB not plotting zero values and improperly displaying legends. Suggestions included modifying the data to include small non-zero values or transposing the data frame, which ultimately resolved the plotting issue. However, the user still seeks a solution to ensure all x-axis labels are displayed correctly. The conversation highlights common challenges in data visualization with MATLAB and potential workarounds.
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

Back
Top