Grouping bar3 plots like bar - matlab

I'm wondering if it is possible to get the same grouping behavior of bar plots into bar3.
For example, if you plot
bar(rand(3));
you get 3 bars for each point; bar groups the different y values for each x. Now I would like to do the same with a 3D data. That is that I have several slices of 2D data that I want to visualize as groups of bars. Then, if my data is
data = rand(3,3,2);
I would like to see data(1,1,:) as a group of bars, and data(1,2,:) as another group and so on.
Is it possible? I cannot find a way of achieving this.
Edit: I'm adding more details, to explain it better.
Lets said that we have two, or more, sets of data {x_(i,j)^s}. What I need is to group in the same grid position (i,j), all the sets s. In this question, they are grouping the data sets side by side, not element-wise, like this:
x1(s1) x1(s2) x1(s3) x2(s1) x2(s2) x2(s3) x3(s1) x3(s2) x3(s3)
x4(s1) x4(s2) x4(s3) x5(s1) x5(s2) x5(s3) x6(s1) x6(s2) x6(s3)
x7(s1) x7(s2) x7(s3) x8(s1) x8(s2) x8(s3) x9(s1) x9(s2) x9(s3)
I would like the bar command behavior, it tends to group when putting more than one data set. I would like to know if it is possible.

I am not sure I fully understood, but if you are looking for grouped behavior like you mention with bar(rand(3)) then you can try
figure; bar3(rand(5),'grouped');
% or maybe
figure; bar3(rand(5),'stacked');
or try to rearrange the data in data so it can work better with bar3 with reshape:
data = rand(3,3,2);
% now each data(i, j, :) will be in single row
changeddata = reshape(data , [size(data , 1)*size(data , 2) 2]);
figure; bar3(changeddata);
figure; bar3(changeddata ,'grouped');
figure; bar3(changeddata ,'stacked');
Maybe if you can give a code example for how it's supposed to look in one group, it would help to understand your question better.

If you want to group bars in 3D bar plots, but you are happy o small groups (let's say 2 or 3 bars each group) then you can simply take advantage of the Y argument in bar3:
BAR3(Y,Z,WIDTH)
so you specify the location of the two groups of bars with two shifted Y vectors.
example:
bar3(0:3:9,rand(4,4),0.3)
hold on
bar3(1:3:10,rand(4,4),0.3)
then you can edit the label the way it suits you.

Related

matlab scatter plot time series

I would like to look at two dimensional data in a time-series - the first idea I had was to use a scatter plot, where you can easily explore timepoint-to-timepoint. Is there a function I could use for this? I looked at scatter3 but it can only plot perfectly-cubic data, not as below:
e.g.
data=rand(5,5,3);
scatter3(data(1,:,:),data(:,1,:),data(:,:,1)) %throws an error
thanks
edit: Originally I had something like >this< in mind
scatter3 seems to be for 3D plots, but you say your data is 2D.
For a simple time-series graph you could presumably even just use plot:
figure
nPoints = 25;
dataX = 1:nPoints;
dataY = rand(1,nPoints);
plot(dataX,dataY, 'o-')
However, the example you give in your link looks like something else, so it seems like scatter (rather than scatter3) might be what you're after. Maybe something like this?
figure
nPoints = 25;
dataX = 1:nPoints;
dataY = rand(1,nPoints);
dataArea = rand(1,nPoints)*100;
dataColours = rand(nPoints,3);
scatter(dataX,dataY,dataArea,dataColours)
EDIT:
I think I understand better what you mean, sorry I didn't see the buttons at the bottom of the link, but correct me if I'm wrong. So you have a set of XY coordinates for multiple objects at different points in time, and ideally you want to plot how the XY coordinates of each object (in 2 dimensions) change over time (in 3 dimensions). Your initial approach in using scatter3 was to try and make a simple 3d graph, but maybe ideally you want a 2d graph that can be either animated or interactive, to change the time point displayed at any given time?
Going back to your original question, I think the issue with your attempt to use scatter3 (or plot3 might be useful too) is I'm not sure what your dummy data would represent. You created data as a 5x5x3 matrix, and I assume that might represent 25 data points, at 3 different time intervals? However, which data would represent the X and which the Y coordinates? It would work with something like the following, where each variable represents the X/Y/Z coordinates of 6 objects (columns) at 5 different time points (rows)
myX = rand(5,6,1);
myY = rand(5,6,1);
% I'm making each time point increase linearly.
myZ = repmat((1:size(myX,1))', 1, size(myX,2));
plot3(myX, myY, myZ, 'o-')
grid on
% Note I find the default dimensions it treats as X, Y and Z unintuitive (e.g. the Z dimension is the vertical dimension), but one could swap the input arguments around to overcome this.
However, especially if you have a lot of points, I'm not sure how clear a graph like this will be, especially compared to the example in your link.
Instead it seems like you ideally want only the XY coordinates of all objects to be plotted for only one time point at once, and a way to cycle through each time point sequentially. This seems trickier, and maybe someone else will be able to answer better than I have. A couple more questions though that might be useful:
How much do you care about the smoothness of the transition. In the example link the circles move smoothly from one position to another, rather than just jumping/teleporting between points.
Ideally do you want a function that would produce an 'animation', cycling through all the time points from begining to end, or a way of manually specifying/changing which time point is being displayed. If the former, maybe this function would be useful (though I've tried it myself yet) https://uk.mathworks.com/matlabcentral/fileexchange/42305-multicomet

Visually Comparable Plot

So I have to plot certain data (90 sets total) and a single set looks like this.
However when I hold on and plot 90 sets superimposed, it looks just like a patch of multiple colours.
Now what would be the most optimal way to represent the plots that can let us compare them and study the difference. For example (and this is just my thought and I am open to opnions) how can I compare these 90 plots in a Matrix fashion viz.
Is there even better ways to represent such collection of plots instead of just superimposing them?
EDIT: To clear things up, I have 90 graphs that look similar to the first graph and I have to compare them in say, a single page. What would be the best way to do it? Also is subplot the best idea for 90 graphs?
Thanks.
You need subplot(), which allows you to plot multiple figures in one window.
http://www.mathworks.com/help/matlab/ref/subplot.html?requestedDomain=www.mathworks.com

Use bar3 instead of bar to plot multiple data in Matlab

I have 3 matrices A, B and C where each of them is of size 21x2. I use bar to plot each one separately. I'm wondering how I can plot the three together using bar3?
So using this code:
A=rand(21,2);
B=rand(21,2);
C=rand(21,2);
fig=figure();b1=bar(A);
fig2=figure();b2=bar(B);
fig3=figure();b3=bar(C);
will generate these three figures:
A:
B:
C:
And what I want to do is that I want them to be all the the same figure but plotted behind each other in the z direction to be something like this
The idea is to create new variables that contain all the data you want in each row intercalated with NaNs. Just that change gives you almost the solution.
for ii=1:size(A,1)
A1((ii-1)*3+1)=A(ii,1);
A1((ii-1)*3+2)=A(ii,2);
A1((ii-1)*3+3)=NaN;
B1((ii-1)*3+1)=B(ii,1);
B1((ii-1)*3+2)=B(ii,2);
B1((ii-1)*3+3)=NaN;
C1((ii-1)*3+1)=C(ii,1);
C1((ii-1)*3+2)=C(ii,2);
C1((ii-1)*3+3)=NaN;
end
h=bar3(horzcat(A1',B1',C1'))
However, I am guessing that you also want to modify the colors.
To do this, the idea is that you can get the colour data for each bar row using get(h(nrow),'Cdata').
with this trick and your own colormap you should be able to colour the bars independently. It is not straightforward, but where's the fun if its easy!

Overlap plots in MATLAB

I currently have a contour plot, just like this one here
http://www.originlab.com/www/helponline/Origin/en/mergedProjects/Tutorial/images/Contour_Plot_with_Major_and_Minor_Levels_Filled_by_Using_Color_Palette/Graph_Gallery_Contour_Plot_Palette_16.png,
now I want to add some simple plots of functions in the length width plane, like f(width) = width^n and so on, but I don't know how to overlap these two plots.
So, you want to retain the current graph when adding new graphs: use hold on. See http://www.mathworks.nl/help/matlab/ref/hold.html.

Bar graph x-axis titles do not work with many bars

I'm using MATLAB and I want a bar plot.
I plotted the bars, and then give titles to the bars.
If I only using 10 bars it works (see pic1).
But often I need more, mostly 32 bars (see pic2).
In this case the titles are not under the bars. I found out that MATLAB don't overlap the titles. The max value is 15 bars.
Does anyone knows how I can get MATLAB to overlap the titles?
Here is my code for plotting the bars:
y = absSlopes(1, 1:size(absSlopes,2));
hb = bar(y);
set(gca,'XTickLabel',titles);
rotateXLabels(gca, 45);
ylabel('Anzahl');
absSlopes is a matrix and I use the complete first line.
You may want to try XTick in addition to XTickLabel. At least for regular plots you can enforce specific spacings through this setting.
You can do this by an additional setup-command:
set(gca,'XTickLabel',titles);
set(gca,'XTick',absSlopes(1,:));
Of course, you can set both properties in the same line, but I like to keep it simple in case you want to deactiviate/comment one or the other setting later on.