How to put several 2D lines in a 3D grayscale plot

How to put several 2D lines in a 3D grayscale plot

It is sometimes convenient to display several 2D curves on one 3D graph and choose gray-scale colors so that the printed version of the figure is readable. The following Matlab code (an example of a loudspeaker’s low-frequencies pressure outputs for different quality factors $Q_{ts}$) shows how to do it.

clc; close all; clearvars

f = 10:10:500; % frequency axis
w0 = 2*pi*200; % angular frequency
Qts = 1:0.5:4; % different Qts
for k=1:length(Qts)
    % calculate the relative pressure at low frequencies
    P(k,:) = 10*w0.*(1i*2*pi*f).^2./((1i*2*pi*f).^2 + (w0/Qts(k))*(1i*2*pi*f) + w0^2);
end
    
%% prepare 3D figure
figure;
[F,Y] = meshgrid(f,Qts); % prepare the X-Y grid
c1 = linspace(0.5,1,length(Qts)); % prepare colors (grayscales)
c2 = linspace(0.1,0.5,length(Qts)); % prepare edge-colors (grayscales)

%% PLOT 3D figure
for kk=1:length(Qts)
    % prepare empty matrix of NaN (Not-a-Number)
    PtotX = NaN(size(P)); 

    % set only the current curve to be plotted (for a given position rx(k))
    PtotX(kk,:) = P(kk,:);

    % plot the curve in 3D and save its handles to "h"
    h(kk) = waterfall(F,Y,20*log10(abs(PtotX))); hold all

    % modify colors by handles "h" 
    set(h(kk), 'FaceColor', c1(kk)*[1 1 1]); 
    set(h(kk),'EdgeColor',c2(kk)*[1 1 1]);
end

xlabel('Frequency [Hz]');
ylabel('Qts [-]');
zlabel('Sound Pressure Level [dB SPL]');
zlim([60 100]); % set limits of the z-axis

%% following modifications can be applied
% set(gca,'YDir','reverse') % flip the y-axis
% set(gca,'View',[-45 40]) % change the camera view

Leave a Reply

Your email address will not be published. Required fields are marked *