简体   繁体   中英

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 . I want to be able to change the color of the bars to represent the data in another 32x32 matrix called CrossCorrX . So that I actually represent 4d-data.

I've tried to access ZData of the bar, that I get from plotting with bar3(LagsX) and replacing the ZData from bar3(CrossCorX) . 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; .

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.

I'm using 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. 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.

To overcome this you can simply assign a figure to each of the bar3 plot and that should work.

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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