How to set x and y values when using bar3 in Matlab?
Asked Answered
E

1

10

Quick version

How can I control the x- and y-values for a 3-d bar plot in Matlab?

Details

Say we have an 10 x 20 data matrix and we plot it using bar3, and we want to set the x- and y-values. For instance:

foodat = rand(10,20);
xVals = [5:14];
yVals = [-3:16];
bar3(xVals, foodat);
xlabel('x'); ylabel('y');

Is there a way to feed it the yVals as well? Otherwise the y axes always defaults to [1:N].

Note I don't just want to change the labels using XTickLabel and YTickLabel. I need to change the actual values on the axes, because I am plotting multiple things in the same figure. It isn't enough to just change how the (wrong) axis ticks are labeled. So this is different from issues like this:

How can I adjust 3-D bar grouping and y-axis labeling in MATLAB?

Other things I have tried

When I try changing the xvals with:

set(gca,'XTick', xVals)
set(gca,'YTick', yVals)

The values are taken in, but actually show up on the wrong axes, so it seems x and y axes are switched using bar3. Plus, it is too late anyway as the bar graph was already plotted with the wrong x- and y-values, so we would end up giving ticks to empty values.

Note added

Matlab tech support just emailed me to let me know about the user contributed function scatterbar3, which does what I want, in a different way than the accepted answer:

http://www.mathworks.com/matlabcentral/fileexchange/1420-scatterbar3

Encincture answered 11/3, 2015 at 15:54 Comment(6)
My guess is that if you are going to plot lots of things together its OK to have some empty 1:4 values. Actually, you could call axis([xmin xmax ymin ymax zmin zmax]) to cahgne the range plotted.Liddell
Yes I wish that were the main problem!Encincture
hum? can you post some images of what you have/ what you want them? I might have misunderstood your problem.Liddell
Ander: I just want to control the x and y values, the way you would with scatter3. That's the main issue (the 'title' issue, so to speak). I'll cut some of the fluff from my question so that is more clear.Encincture
Nice question by the way. Very nice.Liddell
Why not use hist3? It is much better documented and developed than bar3.Encincture
L
6

I found a way of doing it. Ill give you a piece of code, then you'll need to "tidy up" , mainly the axis limits and the Xticks, as bar3 does set up the Xticks inside, so if you want others you'll need to set them manually yourself.

So the trick here is to get the Xdata from the bar3 handle. The thing here is that it seems that there is a handle for each row of the data, so you need to iterate for each of them. Here is the code with the current output:

foodat = rand(20,10);
xVals = [5:14];
yVals = [-3:16];


% The values of Y are OK if called like this.
subplot(121)
bar3(yVals, foodat); 

subplot(122)
h=bar3(yVals, foodat); 
Xdat=get(h,'XData');
axis tight
% Widdth of barplots is 0.8
for ii=1:length(Xdat)
    Xdat{ii}=Xdat{ii}+(min(xVals(:))-1)*ones(size(Xdat{ii}));
    set(h(ii),'XData',Xdat{ii});
end



axis([(min(xVals(:))-0.5) (max(xVals(:))+0.5) min(yVals(:))-0.5, max(yVals(:))+0.5]) 

enter image description here

Note: Y looks different but is not.

As you can see now the X values are the ones you wanted. If you'd want other size than 1 for the intervals between them you'd need to change the code, but you can guess how probably!

Liddell answered 11/3, 2015 at 16:43 Comment(2)
It works! Excellent trick. Note to fix the x ticks (it only shows 5-10 right now), just add set(gca,'XTick', xVals)Encincture
@neuronet exactly. I just didnt want to give you everything done :PLiddell

© 2022 - 2024 — McMap. All rights reserved.