How to set x and y values โ€‹โ€‹when using bar3 in matlab?

Quick version

How can I control the x and y values โ€‹โ€‹for a 3-D line chart in Matlab?

More details

Suppose we have a 10 x 20 data matrix and we build it with bar3 , and we want to set the x and y values. For example:

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

Is there a way to submit and yVals? Otherwise, the y axis always defaults to [1: N].

Note. I do not just want to change tags using XTickLabel and YTickLabel . I need to change the actual values โ€‹โ€‹on the axis, because I draw several things in the same figure. It is not enough just to change how the labels (incorrect) of the axis are marked. So this is different from questions like:

How to set up 3D bar grouping and Y axis marking in MATLAB?

Other things I've tried

When I try to change xvals with

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

The values โ€‹โ€‹are taken, but are actually displayed on the wrong axes, so it seems that the x and y axes are switched using bar3. Also, it's still too late, since the histogram has already been plotted with the wrong x and y values, so we will eventually give ticks for the null values.

Note added

Matlab tech support just emailed me to inform me of a scatterbar3 custom function that does what I want differently from the accepted answer:

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

+7
matlab matlab-figure
source share
1 answer

I found a way to do this. I will give you a piece of code, then you will need to "clean up", basically the limits of the axis and Xticks, since bar3 installs inside Xticks, so if you want others, you will need to install them manually yourself.

So the trick is to get Xdata from the bar3 handle. The thing is, it seems that there is a descriptor for each row of data, so you need to iterate over 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 no.

As you now see, the X values โ€‹โ€‹are the ones you wanted. If you need a different size than 1 for the intervals between them, you will need to change the code, but you can guess how this is possible!

+3
source share

All Articles