필터 지우기
필터 지우기

2D color map shows the image correct but the Y scale is flipped.

조회 수: 16 (최근 30일)
Sneha Kandapal
Sneha Kandapal 2023년 10월 20일
댓글: Sneha Kandapal 2023년 10월 21일
Hello Community,
I wrote a code for plotting 2D color map. Though it works and get the image of what I wnat but the Y scale is flipped. The Y axis is in logarithmic scale and shows the scale from positive to negative instead of negative to positive. I tried multiple ways to flip it but nothing happens. I have also attached an image of how the Y scale should look like.
% Prompt the user to select multiple data files
[dataFiles, dataDirectory] = uigetfile('*.txt', 'Select data files', 'MultiSelect', 'on');
% Check if the user canceled the file selection
if isequal(dataFiles, 0)
disp('No files selected. Exiting...');
return;
end
% Prompt the user to select an output directory
outputDirectory = uigetdir('Select an output directory', 'Specify Output Directory');
% Check if the user canceled the output directory selection
if outputDirectory == 0
disp('No output directory selected. Exiting...');
return;
end
% Initialize variables to store data
xData = [];
yData = [];
% Loop through selected data files
for i = 1:length(dataFiles)
% Construct the full file path
fullFilePath = fullfile(dataDirectory, dataFiles{i});
% Read the data from the current file
data = dlmread(fullFilePath);
% Extract x and y data (1st and 4th columns)
x = data(:, 1);
y = data(:, 4);
% Apply the formula to the y data
y = (10.^((y - 3.51307) / 0.22845)) ./ (7.75E-5 * 0.2);
% Append data to the storage variables
xData = [xData; x];
yData = [yData; y];
end
% Define the number of bins for the histogram
numXBins = 200;
% Adjust as needed
numYBins = 100; % Adjust as needed
% Define the bin edges for the X and Y axes
xBinEdges = linspace(min(xData), max(xData), numXBins);
yBinEdges = logspace(log10(min(yData)), log10(max(yData)), numYBins);
% Create a 2D histogram color map with specific bin edges
h = histcounts2(xData, yData, xBinEdges, yBinEdges);
% Create a color map using imagesc with the Y-axis and scale reversed
figure;
imagesc(xBinEdges, flip(log10(yBinEdges)), log(h)'); % Reverse the Y-axis and its scale
colormap('jet'); % Use the 'jet' colormap for colorful visualization
colorbar; % Display the color bar for the Z-axis (point density)
% Customize the plot appearance
xlabel('X-Axis (Column 1)');
ylabel('Log(Y-Axis)'); % Y-axis is logarithmic and reversed
title('2D Histogram Color Map of Multiple Data Files');
% Specify the output image file path
outputImageFilePath = fullfile(outputDirectory, 'histogram_colormap.png');
% Save the color map plot as an image (e.g., PNG)
saveas(gcf, outputImageFilePath, 'png');
% Display the path where the image is saved
disp(['2D Histogram Color Map saved to: ' outputImageFilePath]);
Editor's note: reformatted unreadable one-lined code block

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 10월 20일
편집: Dyuman Joshi 2023년 10월 20일
Using the high level version of imagesc changes the direction of y-axis to 'reverse'.
Revert it back to normal -
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(C)
set(gca, 'YDir', 'normal')
  댓글 수: 5
DGM
DGM 2023년 10월 21일
You can try either:
% Create a pseudocolor plot using pcolor with reversed Y-axis scale
pcolor(X, Y, imresize(log(h).',size(X)));
shading interp; % Use interpolated shading for smooth color transitions
or
imagesc(imresize(log(h).',size(X)),'xdata',X(:),'ydata',Y(:));
set(gca,'ydir','normal')
pcolor() will render NaNs differently than imagesc() does.
Sneha Kandapal
Sneha Kandapal 2023년 10월 21일
Thank you that worked. I just have to chnage Y to flip(Y).
pcolor(X, flip(Y), imresize(log(h).', size(X)));

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 10월 20일
Use 'reverse' option for the plot axis value direction, e.g.:
t=linspace(0,1.2, 1e3);
dt = t(2)-t(1);
Fs = 1/dt;
f1 = 5;
f2 = 25;
f3 = 100;
Z = sin(2*pi*t*f1)+0.75*sin(2*pi*t*f2)+0.5*cos(2*pi*t*f3)+0.25*randn(size(t));
[P,ff,tt] = pspectrum(Z,Fs,'spectrogram');
figure(1)
waterfall(ff,tt,P')
xlabel('Frequency (Hz)')
ylabel('Time (seconds)')
FIG = gca;
% X and Y axis values are in the reverse order
FIG.XDir = 'reverse'; % This reverses the values of x axis
FIG.YDir = 'reverse'; % This reverses the values of y axis
view([30 45])
% Compare to this one: all axis values are in default order given by MATLAB
% plot tools/fcns
figure(2)
waterfall(ff,tt,P')
xlabel('Frequency (Hz)')
ylabel('Time (seconds)')
view([30 45])

Image Analyst
Image Analyst 2023년 10월 21일
Use either
axis ij
or
axis xy
until it's the way that you want it.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by