How to show multi data probability densities in one axes?
조회 수: 7 (최근 30일)
이전 댓글 표시
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1622888/image.png)
댓글 수: 0
답변 (2개)
Maneet Kaur Bagga
2024년 2월 21일
편집: Maneet Kaur Bagga
2024년 2월 21일
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!
댓글 수: 0
Adam Danz
2024년 2월 21일
편집: Adam Danz
2024년 3월 18일
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)';
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!