How to show multi data probability densities in one axes?

4 visualizzazioni (ultimi 30 giorni)
Say I have lots of samples, (x, y). To show the probability density, I can use binscatter(x, y). The transparency gives the density. A example from MATLAB documentation is below.
But how to show 2 kinds of samples in one axes? Given (x, y),
x = randn(1e4, 1);
y = randn(1e4, 1);
figure;
h1 = binscatter(x, y);
hold on
h2 = binscatter(x+1, y+2);
I prefer different colors, so the differences can be shown clear, both densities and peaks.But it seems binscatter has no propertity as color.
Is there any other way to show 2 kinds of samples in one axes?

Risposte (2)

Maneet Kaur Bagga
Maneet Kaur Bagga il 21 Feb 2024
Modificato: Maneet Kaur Bagga il 21 Feb 2024
Hi,
As per my understanding, you want to visualize the probability densities of the two different sets of samples on the same plot for comparison to clearly see the difference between the densities and peaks of the two plots.
One of the possible ways to do this is to use "contour" function to overlay the density contours of one dataset on top of the other. This will allow you to see the peaks and densities of both the datasets simultaneously, with one represented by a filled "color map" and the other by "contour" lines.
Use "histcounts2" to compute the 2D histograms for both datasets. Then normalize the histograms to ensure they are on the same scale. Plot the first histogram using "imagesc" with a colormap. Then overlay the second histogram with contour lines using "contour", which can be set to a contrasting colour for clear visibility. The number of contour levels can be adjusted in the contour function call. Please refer to the code snippet below for better understanding:
% Generate sample data
x = randn(1e4, 1);
y = randn(1e4, 1);
% Define the grid for the histograms
nbins = 100;
xi = linspace(min(x)-1, max(x)+1, nbins);
yi = linspace(min(y)-1, max(y)+1, nbins);
% Calculate 2D histograms
[hist1, xi, yi] = histcounts2(x, y, xi, yi);
[hist2, ~, ~] = histcounts2(x+1, y+2, xi, yi);
% Normalize histograms to the same scale
maxHist = max(max(max(hist1), max(hist2)));
hist1 = hist1 / maxHist;
hist2 = hist2 / maxHist;
% Create figure
figure;
% Use imagesc to show the first dataset as a color map
imagesc(xi, yi, hist1');
axis xy; % Correct the axis orientation
colormap('hot'); % Choose a colormap for the first dataset
colorbar;
hold on;
% Use contour to overlay the second dataset
[~, hContour] = contour(xi(1:end-1), yi(1:end-1), hist2', 8, 'LineWidth', 2);
hContour.LineColor = 'cyan'; % Choose a color for the second dataset contours
% Add labels and title
xlabel('X-axis');
ylabel('Y-axis');
title('Density and Peaks of Two Datasets');
% Release the hold on the axes
hold off;
Please refer to the MATLAB Documentation for further understanding:
Hope this helps!

Adam Danz
Adam Danz il 21 Feb 2024
Modificato: Adam Danz il 18 Mar 2024
Plotting two binscatters in one axes
This solution does the following:
  • overlay two axes in tiledlayout
  • link the axes limits
  • plots each binscatter in a different axes with a different colormap
  • Assigns a FaceAlpha <1 to the bin scatters to add partial transparency
  • Assigns two colormaps to the tiledlayout
This solution also uses the sky colormap, which became available in R2023a, to generate the blue and red colormaps.
x = randn(1e4, 1);
y = randn(1e4, 1);
figure;
tcl = tiledlayout(1,1);
ax1 = axes(tcl);
ax2 = copyobj(ax1,tcl);
cmap1 = sky(256); % R2023a
h1 = binscatter(ax1,x,y,'FaceAlpha',0.6);
colormap(ax1,cmap1)
cmap2 = cmap1(:,[2 1 1]);
h2 = binscatter(ax2,x+2,y+2,'FaceAlpha',0.6);
colormap(ax2,cmap2)
linkaxes([ax1,ax2])
axis(ax2,'off') % or, set ax2.Color='none' to verify alignment
cb1 = colorbar(ax1);
cb1.Layout.Tile = 'east';
cb1.Label.String = 'Time (s)';
cb2 = colorbar(ax2);
cb2.Layout.Tile = 'east';
cb2.Label.String = 'Concentration (M)';

Categorie

Scopri di più su Data Distribution Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by