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

In summary, the conversation is about creating a multi bar graph in MATLAB using a 6 x 16 data frame that represents 16 different soil types at 6 different depths. The attempt at a solution involves using the "bar" function and setting the X and Y vectors correctly. A small hack is suggested to make the graph display correctly. The issue of the x-axis labels not displaying correctly is still unresolved.
  • #1
madtraveller
28
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
  • #2
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.
 
  • #3
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
 
  • #4
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
 
  • #5
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.
 
  • #6
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
 

What is a multi bar plot in Matlab?

A multi bar plot in Matlab is a type of graphical representation that displays multiple bars side by side, allowing for comparison of multiple data sets. It is commonly used to show the relationship between two or more variables, such as comparing the sales of different products over a period of time.

How do I create a multi bar plot in Matlab?

To create a multi bar plot in Matlab, you can use the bar function. This function takes in two arrays, one for the x-axis values and one for the y-axis values, and automatically creates the bars for each value. You can also customize the appearance of the plot by specifying the color, width, and spacing of the bars.

Can I plot multiple groups of bars in the same figure?

Yes, you can plot multiple groups of bars in the same figure by using the hold on command. This will allow you to add additional bars to the existing plot without overwriting it. You can also use the legend function to label each group of bars for better visualization.

How do I add error bars to a multi bar plot in Matlab?

To add error bars to a multi bar plot in Matlab, you can use the errorbar function. This function takes in the same input parameters as the bar function, but also allows you to specify the error values for each bar. This can be useful for displaying the variability or uncertainty in your data.

Can I save my multi bar plot in Matlab as an image?

Yes, you can save your multi bar plot in Matlab as an image by using the saveas function. This function allows you to specify the file format (e.g. PNG, JPEG, PDF) and the resolution of the image. You can also use the print function to save the plot directly to a printer or a file.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
Back
Top