简体   繁体   English

如何更改bar3(MATLAB)中的颜色表示以表示来自另一个变量的数据?

[英]How to change color representation in bar3 (MATLAB) to represent data from another variable?

I have the 32x32 matrix LagsX which contains data that I want to plot in 3d using bar3 . 我有32x32矩阵LagsX ,其中包含要使用bar3在3d中bar3 I want to be able to change the color of the bars to represent the data in another 32x32 matrix called CrossCorrX . 我希望能够更改条形的颜色,以表示另一个名为CrossCorrX 32x32矩阵中的数据。 So that I actually represent 4d-data. 这样我实际上代表了4D数据。

I've tried to access ZData of the bar, that I get from plotting with bar3(LagsX) and replacing the ZData from bar3(CrossCorX) . 我尝试访问ZData ,这是通过用bar3(LagsX)绘制并替换来自bar3(CrossCorX)ZData获得的。 But I recieved the following error: 但我收到以下错误:

Invalid or deleted object. 无效或已删除的对象。

This is the code I tried to use: 这是我尝试使用的代码:

b1 = bar3(crossCorrX);  colorbar
b = bar3(lagsX); 
for k = 1:length(b)
    b(k).CData = b1(k).ZData;
    b(k).FaceColor = 'interp';
end 

I recieved the error in the line b(k).CData = b1(k).ZData; 我在b(k).CData = b1(k).ZData;行中收到错误b(k).CData = b1(k).ZData; .

Do you have suggestions how to do it? 您有建议怎么做吗? Why does the error appear? 为什么会出现错误? Suggestions for using other functions than bar3 would be appreciated as well. 使用bar3以外的其他功能的建议也将不胜感激。

I'm using MATLAB R2015a. 我正在使用MATLAB R2015a。

EDIT 编辑

I have understood my problem, just needed to add figure; 我已经了解了我的问题,只需要添加数字即可; again. 再次。 My new problem is, that wherever LagsX == 0, the color that represents CrossCorX is displayed as it was zero, even though it is different. 我的新问题是,无论LagsX == 0哪里,代表CrossCorX的颜色都会显示为零,即使它是不同的。 Any tips of how can I overcome this? 关于如何克服这个问题的任何提示?

Thanks ahead! 谢谢你!

I think the error is occurring because you are overwriting the 1st plot with the 2nd, hence its ZData property is not accessible anymore. 我认为发生错误是因为您正在用第二个覆盖第一个图,因此无法再访问其ZData属性。

To overcome this you can simply assign a figure to each of the bar3 plot and that should work. 为了克服这个问题,您只需为每个bar3图分配一个图形即可。

Example with random data: 随机数据示例:

clear
clc
close all

A = rand(10,10);
A2 = rand(10,10);

figure(1)
b1= bar3(A);

figure(2)
b = bar3(A2);

hcb = colorbar;

for k = 1:length(b)
    b(k).CData = b1(k).ZData;
    b(k).FaceColor = 'interp';
end

Output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM