在 Matlab 中使用 bar3 时如何设置 x 和 y 值? [英] How to set x and y values when using bar3 in Matlab?

查看:55
本文介绍了在 Matlab 中使用 bar3 时如何设置 x 和 y 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速版

如何在 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');

有没有办法将 yVals 也喂给它?否则 y 轴始终默认为 [1:N].

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

注意我不只是想使用 XTickLabelYTickLabel 来更改 labels.我需要更改轴上的实际,因为我在同一个图中绘制了多个内容.仅仅改变(错误的)轴刻度的标记方式是不够的.所以这与这样的问题不同:

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:

如何我可以在 MATLAB 中调整 3-D 条形分组和 y 轴标签吗?

我尝试过的其他事情

当我尝试更改 xvals 时:

When I try changing the xvals with:

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

取入值,但实际上显示在错误的轴上,因此 x 和 y 轴似乎是使用 bar3 切换的.另外,无论如何为时已晚,因为条形图已经用错误的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆