2015-08-28 97 views
1

我有一個矩陣中5個不同頻率的數據集,我想用plot,hist和mesh來說明它們。然而,每種情節類型使用不同的顏色映射(見圖片),所以我需要每個情節的圖例。Matlab:用於hist,plot和mesh的相同色圖

有沒有辦法爲所有繪圖類型設置相同的顏色映射,或者爲每個繪圖類型指定一個顏色映射?另一件奇怪的事情:例如,我可以使用圖形工具爲hist設置顏色表,但不能用於正常的plot。對於網格我必須使用hold on循環,所以我猜這裏設置顏色不同於定義顏色映射?

enter image description here

編輯:

這裏是一個小例子。它仍然不起作用,請參閱下面的代碼中的註釋。

clear all; 
close all; 
clc; 

% make up some data with the original format 
freqLen = 5; 
data = zeros(10, 3, 3, freqLen); 
data(:, :, :, 1) = rand(10, 3, 3); 
data(:, :, :, 2) = rand(10, 3, 3)+1; 
data(:, :, :, 3) = rand(10, 3, 3)+2; 
data(:, :, :, 4) = rand(10, 3, 3)+3; 
data(:, :, :, 5) = rand(10, 3, 3)+4; 
% reshape data so we get a vector for each frequency 
dataF = reshape(data, [10*3*3, freqLen]); 

% prepare colors for plot, try to get 5 colors over the range of colormap 
% but I get wrong colors using both methods below! 
%cols = colormap(jet); 
%cols = cols(1:round(length(cols)/length(freqGHz)):end, :); 
cols = jet(freqLen); 

% plot samples in 3D 
figure('Position', [0 0 1000 1000]); 
subplot(211); 
hold on; 
for iF = 1:freqLen 
    dataThisF = dataF(:, iF); 
    data3D = reshape(dataThisF, [10*3, 3]); 
    mesh(data3D); 
    % try to give each "holded" mesh a different color. Not working! 
    % after the loop, all meshes have the last color 
    set(get(gca, 'child'), 'FaceColor', 'w', 'EdgeColor', cols(iF, :)); 
end 
view(60, 20); 

% plot samples 
subplot(223); 
hold on; 
for iF = 1:freqLen 
    % the loop is not avoidable 
    % because matlab maps the colors wrong when plotting as a matrix 
    % at least its not using the colormap colors 
    plot(dataF(:, iF), 'Color', cols(iF, :)); 
end 

% plot histogram 
subplot(224); 
% actually the only one which is working as intended, horray! 
hist(dataF, 50); 

enter image description here

我怎麼可以給holded者網單一的顏色,和別人不同?如何在使用簡單線條繪製矩陣時繪製正確的噴射色彩圖,或者至少從噴射色彩圖中獲取5種顏色(jet(5)給出5種不同的顏色,但不是從頭到尾)?

+0

你能告訴一小段代碼如何創建其中之一嗎?你是否以相同的順序繪製所有數據?即首先10Ghz總是,然後15,... –

回答

2

你在說什麼主要是ColorOrder財產(而不是圖的colormap)。

上面給出的colororder的鏈接將解釋如何強制Matlab對所有圖使用給定的一組顏色。它適用於plot。您不需要循環,只需在繪圖前定義圖的DefaultColororder屬性,然後在一次調用中繪製所有系列,Matlab將根據您之前定義的順序爲每個繪圖分配顏色。

meshhist它不是簡單的不幸,所以你必須要運行一個循環指定顏色或每個圖形對象。要在創建圖形對象屬性(如顏色)後修改它,必須使用set方法,或者如果使用Matlab版本> = 2014b,則必須使用直接點符號。對於這兩種方法,你需要有圖形對象的handle,所以當你知道你需要的時候最容易的就是在創建時檢索圖形對象句柄*。

* 而不是骯髒的黑客就像get(gca, 'child')。這很容易出錯,事實上在你的情況下是錯誤的。您的代碼無法正確着色,因爲您沒有以這種方式獲得正確的圖形處理。

下面的代碼繪製所有圖形,檢索每個圖形對象的句柄,然後在最後一個循環中分配顏色。


%// Get a few colors 
cols = jet(freqLen); 

% plot samples in 3D 
figure('Position', [0 0 1000 1000]); 
set(gcf , 'DefaultAxesColorOrder',cols) %// set the line color order for this figure 

subplot(2,1,1,'NextPlot','add');   %// 'NextPlot','add' == "hold on" ; 
for iF = 1:freqLen 
    dataThisF = dataF(:, iF); 
    data3D = reshape(dataThisF, [10*3, 3]); 
    h.mesh(iF) = mesh(data3D) ;   %// plot MESH and retrieve handles 

    %// You can set the color here direct, or in the last final "coloring" loop 
    %// set(h.mesh(iF) , 'FaceColor', 'w', 'EdgeColor', cols(iF, :)); 
end 
view(60, 20); 

%// plot samples 
subplot(223); 
h.plots = plot(dataF);     %// plot LINES and retrieve handles 

%// plot histogram 
subplot(224); 
[counts,centers] = hist(dataF, 50) ; %// get the counts values for each series 
h.hist = bar(centers,counts) ;   %// plot HISTOGRAM and retrieve handles 

%// now color every series with the same color 
for iF = 1:freqLen 
    thisColor = cols(iF, :) ; 
    set(h.mesh(iF) , 'EdgeColor' , thisColor , 'FaceColor', 'w'); 
    set(h.hist(iF) , 'EdgeColor' , thisColor , 'FaceColor' , thisColor) 
    %// this is actually redundant, the colors of the plots were already right from the 
    %// beginning thanks to the "DefaultColorOrder" property we specified earlier 
    set(h.plots(iF) , 'Color'  , thisColor) 
end 

將會使你在下圖中: colororder

+0

哇!整蠱,謝謝:) – Azial

0

UPDATE:原來的問題問

有沒有一種方法來設置相同的顏色表的所有情節類型,或指定一個爲他們每個人的?

這個答案,回答這個問題,色彩地圖意味着colormap在MATLAB字面上。


您可以使用colormap設置圖形的顏色表。您可以使用許多內置色彩貼圖之一或指定您自己的色彩貼圖。


使用mesh和內置hsv顏色表的一個例子可能是

figure; 
mesh(data); 
colormap(hsv); 

這將適用基於顏色表上

MATLAB hsv color

到你的身材。您也可以創建自己的顏色表像

map = [1, 0, 0, 
     1, 1, 1, 
     0, 1, 0, 
     0, 0, 0]; 
colormap(map); 

這將創建一個顏色表與顏色

MATLAB Flag colormap


的MATLAB文檔包含有關使用colormap廣泛的信息。

+0

嗯,我不認爲他正在尋找一個色彩映射,因爲MATLAB定義它。我想他希望所有單獨的圖表中的每個數據範圍都具有相同的顏色(例如,所有圖中的10GHz藍色,20GHz紅色等) – Adriaan

+0

謝謝,這就是我發現的。但是,根據高度設置網格的顏色映射表將對所有網格進行着色。我想我必須使用set(cga,...)或其他東西來獨立設置邊緣顏色。使用set(0,'DefaultAxesColorOrder',jet(5))更改線圖的顏色似乎也不同。 – Azial

+0

@Azial編輯更清晰,但問題仍然有點模糊。我拿了「是否有一種方法可以爲所有的繪圖類型設置相同的顏色映射表,或者爲每個繪圖類型指定一個?」意思是MATLAB中的'colormap'。我編輯了我的答案,明確提到它的答案。我將把它留在這裏以防將來有人需要它,因爲我感覺它仍然回答了問題的標題。 – IKavanagh