在Matlab中使用bar3时如何设置x和y值? [英] How to set x and y values when using bar3 in Matlab?
问题描述
快速版本
如何在Matlab中控制3维条形图的x和y值?
How can I control the x- and y-values for a 3-d bar plot in Matlab?
详细信息
假设我们有一个10 x 20的数据矩阵,并使用bar3
对其进行绘制,并且我们想要设置x和y值.例如:
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');
是否也有办法为其提供yVal?否则,y轴始终默认为[1:N].
Is there a way to feed it the yVals as well? Otherwise the y axes always defaults to [1:N].
请注意,我不只是想使用XTickLabel
和YTickLabel
更改标签.我需要更改轴上的实际值,因为我在同一图中绘制了多个东西.仅更改(错误的)轴刻度标记的方式是不够的.因此,这与此类问题有所不同:
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:
我尝试过的其他事情
当我尝试通过以下方式更改xval时:
When I try changing the xvals with:
set(gca,'XTick', xVals)
set(gca,'YTick', yVals)
这些值已被接受,但实际上显示在错误的轴上,因此似乎使用bar3切换了x和y轴.另外,反正为时已晚,因为条形图已经用错误的x值和y值绘制了,所以我们最终给空值打了勾.
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.
已添加注释
Matlab技术支持部门刚刚通过电子邮件发送给我,让我了解了用户提供的功能scatterbar3
,该功能以与接受的答案不同的方式实现了我想要的功能:
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
推荐答案
我找到了一种方法.麻烦给您一段代码,然后您需要整理",主要是轴限制和Xticks,因为bar3
确实在内部设置了Xticks,因此,如果需要其他内容,则需要设置它们自己动手.
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.
因此,这里的技巧是从bar3句柄获取Xdata.这里的问题是,似乎数据的每一行都有一个句柄,因此您需要为它们中的每一个进行迭代.这是当前输出的代码:
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])
注意:Y看起来有所不同,但没什么.
Note: Y looks different but is not.
您现在可以看到X值就是您想要的值.如果您希望间隔除1以外的其他大小,则需要更改代码,但是您可以猜测可能性有多大!
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!
这篇关于在Matlab中使用bar3时如何设置x和y值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!