2D color map shows the image correct but the Y scale is flipped.
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sneha Kandapal
am 20 Okt. 2023
Kommentiert: Sneha Kandapal
am 21 Okt. 2023
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1516681/image.png)
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]);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1516676/image.jpeg)
Editor's note: reformatted unreadable one-lined code block
0 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 20 Okt. 2023
Bearbeitet: Dyuman Joshi
am 20 Okt. 2023
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 Kommentare
DGM
am 21 Okt. 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.
Weitere Antworten (2)
Sulaymon Eshkabilov
am 20 Okt. 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])
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!