Effacer les filtres
Effacer les filtres

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

16 vues (au cours des 30 derniers jours)
Sneha Kandapal
Sneha Kandapal le 20 Oct 2023
Commenté : Sneha Kandapal le 21 Oct 2023
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

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 20 Oct 2023
Modifié(e) : Dyuman Joshi le 20 Oct 2023
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 commentaires
DGM
DGM le 21 Oct 2023
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 le 21 Oct 2023
Thank you that worked. I just have to chnage Y to flip(Y).
pcolor(X, flip(Y), imresize(log(h).', size(X)));

Connectez-vous pour commenter.

Plus de réponses (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 20 Oct 2023
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 le 21 Oct 2023
Use either
axis ij
or
axis xy
until it's the way that you want it.

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by