Better Method to Plot a Series of Bar Charts in Matlab - matlab

I have a 4x3 bar charts which I save as figures then I combine them in one image after that in latex. The problem is that the barcharts are two small, and I'm wondering if there is any Matlab type of figure that would be better on showing my results. Also I'm wondering if there is a way to save it as a one figure in Matlab so that I don't have to combine them in latex?
This is an example on how it looks like in latex:

So you can easily combine the scenario and trials using a scatter plot. Suppose each column in your bar was labelled a, b, and c. Then if you have the following data:
a = [90 95 98]';
b = [20 25 29]';
c = [50 40 30]';
You can create an index matrix labelling which column it should go into.
t = [1 1 1 2 2 2 3 3 3]';
Then create the scatter plot with * markers. You can even use different different colors or markers built into Matlab to denote different trials/conditions/methods/input.
scatter(t,[a; b; c], '*');
You can then set the labels as follows.
set(gca,'Xtick',1:4,'XTickLabel',{'a', 'b', 'c', 'd'})
Here is the resultant plot.

Related

Plot the stacked bar graph in MATLAB with original values instead of a cumulative summation

By original values, I mean that when I create the stacked bar graph, MATLAB automatically appends values to its previous ones to create a cumulative summation. This is illustrated by taking an example from MATLAB official site, whose screenshot is shown below
Here first the value 2 is plotted, then 2+2 and then 2+2+3. What if we have to plot the values as they are, which means plotting [2 2 3] instead of [2 4 7]. Also, I want to plot the values given in variable y as a stacked bar graph in above screenshot as if those are real values.
Help, please!
Perhaps you can use a 3D graph and then change the view angle.
y=[2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar3(y)
view(-90,0)
The above code was used to generate this graph.

how do you visualize a matrix whose elements are vectors in MATLAB?

I have generated a 3 x 2 x 25 matrix A in MATLAB.
A(1,2,1) = 5 means method 1, type 2, trial 1 has a count value of 5.
A(3,1,2) = 7 means method 3, type 1, trial 2 has a count value of 7.
Basically, there are 25 count values for each (method, type) pair.
In the past, I have used "histogram" MATLAB function to visualize a 2-D frequency plot and I know I can use it here like so:
histogram(A(3,1,:))
But if I use histogram I would have to plot all 6 like:
histogram(A(1,1,:))
histogram(A(1,2,:))
histogram(A(2,1,:))
histogram(A(2,2,:))
histogram(A(3,1,:))
histogram(A(3,2,:))
But I was wondering if there is a way to all 6 plots in a 3-dimensional histogram ?
You want 4 dimensions of information on a 3 dimensional plot. the only good way to get a 4th dimension of information would probably be color and that's going to be a mess with 6 plots. You're better off separating the plots into different graphs.
From what you're describing the second dimension only has 2 states. Would it make sense in your case to have 2 3d plots with this dimension being split across graphs/colors?

plotting graphs with x-axis of desired length in matlab

I want to plot graphs in matlab, In hand, I had two raw data obtained from the market, say at year 0.25,0.5,0.75,1,2,3 and 4, corresponding values of product A are [0.9998,0.997,0.887,0.779,0.661,0.442,0.345] and B are [0.878,0.765,0.662,0.594,0.436,0.304,0.211] respectively. When I use
plot([0.25,0.5,0.75,1,2,3,4],[0.9998,0.997,0.887,0.779,0.661,0.442,0.345],'k+',[0.25,0.5,0.75,1,2,3,4],[0.878,0.765,0.662,0.594,0.436,0.304,0.211],'b*')
However, the graphs produced gives 4 lines. What should be done to fix the problem?
You need to create a graphic handler before modifying the XTick, Use the following
figure
ax = gca ;
plot([0.25,0.5,0.75,1,2,3,4],[0.9998,0.997,0.887,0.779,0.661,0.442,0.345],'k+',[0.25,0.5,0.75,1,2,3,4],[0.878,0.765,0.662,0.594,0.436,0.304,0.211],'b*')
ax.XTick = [0 0.25 0.5 0.75 1 2 3 4];

How to exempt an object from Matlab plot limits calculation

I want to add objects to my matlab plots which have defined x limits but span the whole y range. Examples are vertical lines or shaded regions delimited by two x values. I am aware of the option to use the current plot limits like this:
plot(1:10)
yl = ylim();
% Use y limits of current plot as y values
patch([ 3 3 5 5 ],[ yl(1) yl(2) yl(2) yl(1) ], 'red');
However I want my users to be able to increase plot y limits afterwards (e.g. to synchronize limits of multiple plots) and also want them to continue profiting from Matlab's automatic setting of plot limits.
This would be archivable if I would use the following code to set the y coordinates of my objects to the largest and smallest possible integers, respectively (intmax() and intmin() in Matlab) and tell Matlab not to consider that object during calculation of plot limits.
plot(1:10)
% Make graphical object which spans the whole possibly y range
p = patch([ 3 3 5 5 ],[ intmin intmax intmax intmin ], 'red');
% Does something like the following function exist?
exemptFromPlotLimitsCalculation(p)
Is this possible in Matlab?
You could plot the patch (or fill) really large (for example by using realmax) and exclude it from rescaling by setting the property YLimInclude to off
patch([3 3 5 5], realmax*[ -1 1 1 -1], 'red', 'YLimInclude', 'off');
have a look at this
In the postActionCallback you can resize your patch

plot two signals with different axis and limits in matlab

let's consider i am plotting two signals together on the same graph which has different limits and thus i want different axis
plot(a)
axis ([-2 10 -2 8])
hold 'on'
plot(b)
axis ([-1 4 -4 7])
hold 'off'
where 'a' and 'b' are two signal expression. the problem here is the signals are getting plot but only the second axis is working and plot a is not getting limited to the first specified axis. the reason being the second axis is obviously overwriting the first axes but any idea how to plot both signals with both axis limits?
You can select the data you wish to plot using logical operators.
Let's consider the case for plot a.
Assign each column of bs to a variable:
x1 = bs(:,1)
y1 = bs(:,2)
Then select only the values that meet the condition specified:
xPlot = x1(x1 > -2 & x1 < 10)
yPlot = y1(y1 > -2 & y1 < 8)
Assuming they both contain the same number of elements you can then plot them.
If not, you need to pad the smaller array with Nan, for example, to avoid getting an error about mismatching dimension.
Once you know which array is smaller, you can do it as follows. Let's say in this case xPlot is smaller than yPlot:
m = max(numel(xPlot),numel(yPlot)) %// Just getting the larger dimension
xPlot(numel(xPlot)+1:m) = NaN
Now you can call
plot(xPlot,yPlot,'b-','LineWidth',2)
and that should work. The same applies for the b plot.
Hope that helps!
You may want to have a look at plotyy to get 2 different y-axis.
If there is nothing in common in your plots, maybe you should plot them on 2 different axes, like
figure('Name', 'Example');
subplot(121);plot(rand(3));
subplot(122);plot(rand(3));
UPDATE
If you absolutely need two axes, you may try something like this
figure('Name', 'plotyy');
h = plotyy([0 1 2 4], 0:4, [4 5], [2 1]);
linkaxes(h, 'off');
axis(h(1), [0 4 0 4]);
axis(h(2), [4 5 1 2]);
If what you're looking for is something along the lines of plotyy but in the other direction, look at plotxx function from matlabcentral that does a similar thing in the x-direction.
You may have to tweak it to get it do everything you need, but it will give you a good starting point.
You can use plotyy which will create 2 y axes with different scales and limits.