Clear Filters
Clear Filters

Visualizing line plot with histogram density

6 views (last 30 days)
Mark
Mark on 12 Jul 2023
Answered: Image Analyst on 12 Jul 2023
I have multiples line plots which overlaps each other and I would like to plot my data something like the figure below to represent my data more quantitatively. Please assist me to plot this figure.
This is what I have so far.
% Generate sample data
numProfiles = 300; % Number of temperature profiles
numAltitudes = 101; % Number of altitude points (from 0km to 100km with 1km increment)
altitude = 0:1:100; % Altitude values in km
temperatures = zeros(numAltitudes, numProfiles);
% Generate temperatures with increased variation in specific altitude ranges
baseTemperature = 200 + altitude' * 3; % Base temperature profile
% Generate variations for each profile
for i = 1:numProfiles
variation = zeros(numAltitudes, 1);
% Add varying magnitudes of variation at different altitude points
for j = 1:numAltitudes
if altitude(j) < 20
variation(j) = randn * 50; % Large variation
elseif altitude(j) < 50
variation(j) = randn * 10; % Increased variation
elseif altitude(j) < 70
variation(j) = randn * 3; % Moderate variation
else
variation(j) = randn * 1; % Very small variation
end
end
temperatures(:, i) = baseTemperature + variation;
end
% Create histogram bins to get data density for each slice of altitude
nbins = 40;
bins = linspace( min(temperatures(:)), max(temperatures(:)), nbins );
hc = NaN(numAltitudes,nbins-1);
for i = 1:numAltitudes
hc(i,:) = histcounts(temperatures(i,:), bins);
end
% Get the centre value of each histogram bin
temps = (bins(1:end-1)+bins(2:end))/2;
% Get the x/y values as a mesh to plot the surface
[x,y] = meshgrid( temps, altitude );
% Plotting
figure;
subplot(1,2,1);
hold on;
colors = lines(numProfiles); % Generate a color map with distinct colors
for i = 1:numProfiles
plot(temperatures(:, i),altitude, 'Color', colors(i, :)); % Assign distinct color to each plot
end
% Customize the plot
ylabel('Altitude (km)');
xlabel('Temperature (K)');
grid on;
% Plot a surface with no edges, where the z-direction indicates density
% Viewed from "above", this will look like a heatmap
subplot(1,2,2);
surface(x, y, hc);
ylabel('Altitude (km)');
xlabel('Temperature (K)');
% Create a colour map which fades from white -> blue -> green -> yellow -> red
c = interp1( 1:5, [1,1,1; 0,0.5,1; 0.2,1,0.2; 1,1,0; 1,0,0], linspace(1,5,20) );
colormap(c);
colorbar;
My current figure looks something like this.

Answers (1)

Image Analyst
Image Analyst on 12 Jul 2023
Try this:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Generate sample data
numProfiles = 300; % Number of temperature profiles
numAltitudes = 101; % Number of altitude points (from 0km to 100km with 1km increment)
altitude = 0:1:100; % Altitude values in km
temperatures = zeros(numAltitudes, numProfiles);
% Generate temperatures with increased variation in specific altitude ranges
baseTemperature = 200 + altitude' * 3; % Base temperature profile
% Generate variations for each profile
for i = 1:numProfiles
variation = zeros(numAltitudes, 1);
% Add varying magnitudes of variation at different altitude points
for j = 1:numAltitudes
if altitude(j) < 20
variation(j) = randn * 50; % Large variation
elseif altitude(j) < 50
variation(j) = randn * 10; % Increased variation
elseif altitude(j) < 70
variation(j) = randn * 3; % Moderate variation
else
variation(j) = randn * 1; % Very small variation
end
end
temperatures(:, i) = baseTemperature + variation;
end
% Create histogram bins to get data density for each slice of altitude
nbins = 40;
bins = linspace( min(temperatures(:)), max(temperatures(:)), nbins );
hc = NaN(numAltitudes,nbins-1);
for i = 1:numAltitudes
hc(i,:) = histcounts(temperatures(i,:), bins);
end
% Get the centre value of each histogram bin
temps = (bins(1:end-1)+bins(2:end))/2;
% Get the x/y values as a mesh to plot the surface
[x,y] = meshgrid( temps, altitude );
% Plotting
figure;
subplot(1,2,1);
hold on;
colors = lines(numProfiles); % Generate a color map with distinct colors
for i = 1:numProfiles
plot(temperatures(:, i),altitude, 'Color', colors(i, :)); % Assign distinct color to each plot
end
% Customize the plot
ylabel('Altitude (km)');
xlabel('Temperature (K)');
grid on;
% Get mean temperature
meanTemperature = mean(temperatures, 2)';
% Plot mean temperature over other plots.
hold on;
plot(meanTemperature, altitude, 'k-', 'LineWidth', 4);
% Plot a surface with no edges, where the z-direction indicates density
% Viewed from "above", this will look like a heatmap
subplot(1,2,2);
surface(x, y, hc);
ylabel('Altitude (km)');
xlabel('Temperature (K)');
% Create a colour map which fades from white -> blue -> green -> yellow -> red
c = interp1( 1:5, [1,1,1; 0,0.5,1; 0.2,1,0.2; 1,1,0; 1,0,0], linspace(1,5,20) );
colormap(c);
colorbar;
% Plot mean temperature over surface.
hold on;
plot(meanTemperature, altitude, 'k-', 'LineWidth', 4);
% Maximize window
g = gcf;
g.WindowState = 'maximized';

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!