How to plot a histogram as a scatter plot in matlab? - matlab

The y axis should be frequency (which is the histogram part) while the x axis is the xdata used to plot the histogram. Essentially what I need is exactly a histogram but instead of bars, there should be points.

I'd do that using the histcounts command. It's like the histogram command, but instead of plotting the data, it returns the number in each bin and the bin edges. You can then plot that as points using the general plot command.
x = randn(1,1000); %Generate some data to plot
figure(1); clf;
subplot(2,1,1); hold on;
h = histogram(x,'normalization','probability');
title('Plotted as a histogram');
ylabel('Frequency');
subplot(2,1,2); hold on;
[N, edges] = histcounts(x,'normalization','probability');
centers = (edges(1:end-1) + edges(2:end))./2; %histcounts gives the bin edges, but we want to plot the bin centers
plot(centers,N,'ko');
title('Plotted as points');
ylabel('Frequency');

Related

Remove Exponents from Matlab LOG-LOG plot

I am trying to plot several frequency response curves on a logx and logy plot. I also want the axes to show values without exponents.
I cannot figure out a way to plot the log scale AND remove exponents. Using the following, I can only either set my scale to log, OR remove exponents. Not both.
figure(3);clf;
hold on;
%set(gca,'YScale', 'log', 'XScale', 'log')); %This doesnt work with ax.XAxis.Exponent = 0;
plot(freq, Pxx,'r', 'LineWidth', 1.5, 'DisplayName', 'Single-Sided Spectral Density');
ax=gca;
ax.XAxis.Exponent = 0; %remove exponents
ax.YScale = 'log'; %Log Y
ax.XScale = 'log'; %Log X
xlabel('Frequency [Hz]');ylabel('[WU^2 / Hz]');
title('Average Single-Sided Spectral Density [G_x_x] vs. Frequency')
grid on;
xlim([0 max(freq)]);legend('FontSize',14);
hold off
Plot with log scale, AND Exponents in scale values
Plot without log scale, without exponents in scale values
Changing the tick labels can be done like this:
figure;loglog([12 123 1233],[12 144 1111]);
set(gca,'xticklabel',arrayfun(#(x) num2str(x),(get(gca,'xtick')),'un',0))
set(gca,'yticklabel',arrayfun(#(x) num2str(x),(get(gca,'ytick')),'un',0))

Matlab_ Plot the difference between two graphs

I am new to Matlab and I am struggling plotting the difference between these two graphs (subtract one plot from another) ... Can anyone help me?
% 2D plot of original target locations
X= double(xCoords);
Y= double(yCoords);
originalvalues = hist3([X(:) Y(:)],[30 40]);
imagesc(originalvalues)
contourf(originalvalues)
c= colorbar;
c.Label.String = 'Initial location';
axis equal
axis xy
xlabel('endCoordinatesx');
ylabel('endCoordinatesy');
title('2D Map of Original locations');
% 2D plot of final target locations
Xf= Design.endCoordinatesX;
Yf= Design.endCoordinatesY;
values = hist3(double([Xf(:) Yf(:)],[30 40]));
imagesc(values)
contourf(values)
c= colorbar;
c.Label.String = 'Final location';
axis equal
axis xy
xlabel('endCoordinatesx');
ylabel('endCoordinatesy');
title('2D Map of final locations');
If I understood well your problem, you want to making a third plot representing the difference between the two datasets.
What you have to do is to get common bins for all the histograms you calculate :
% find common centers for the two datasets
[~, centers] = hist3(cat(2,[X(:) ; Xf(:)],...
[Y(:) ; Yf(:)]),...
[30 40]);
% then you can calculate the histogram for each set of data :
originalvalues = hist3([X(:) Y(:) ], centers);
values = hist3([Xf(:) Yf(:)], centers);
% finaly, compute the difference between the two (now the bins are "aligned")
differenceValue = values - originalvalues;

how to fill colors between contours in matlab

I have plotted multiple contours in matlab with the hold on command, using the contour function of MatLab. How can I go ahead if I want to fill color between the first and the last contour. I tried contourf function but it didnt work out that way.
Thanks in advance.
I have written two simple lines which plots the zero level set contour after every iterations.
hold on;
contour(X,Y,phi,[0,0],'r');
This can be done by using the get command to get individual components from the graph. For example:
[x, y, z] = peaks; % Generate some data
figure; surf(x, y, z); % Show
figure;[c, h] = contourf(x, y, z, [0 0]); % Build and show contour plot
patches = get(h, 'children'); % Get different patches
set(patches(1), 'facecolor', 'r') % Colour one red

Hist3 Plotting and Axes Range

I'm plotting a 3-D histogram with MATLAB and it's working pretty ok, except for the different axes ranges. I'd like them to be defined in a way, where equal value pairs lie on the bisecting line.
My code looks like this (more or less "stolen" from the hist3 MATLAB example):
[vec_voxel_ids, vec_dose_values_reference, vec_dose_values_control] = ...
textread('_BOOSTINT_voxel_data.txt', '%u %u %u');
mat_dose_values = [vec_dose_values_reference, vec_dose_values_control];
hist3(mat_dose_values, [100, 100]);
xlabel('Dose Reference');
ylabel('Dose Control');
set(gcf, 'renderer', 'opengl');
set(get(gca,'child'), 'FaceColor', 'interp', 'CDataMode', 'auto');
This is how it looks:
In order to reposition the bins to align their centers the ticks and also choose the range of the bin values you can use hist3's 'edges' option (similar to that in histc):
data = 500+5*randn(1e3,2); % dummy data
d = 1; % width for x and y bins
x = 480:d:520; % range for x bins
y = x; % use same range for y bins, can be different
hist3(data, 'edges', {x-d/2,y-d/2}); % subtract half bin width to center bins
set(gcf, 'renderer', 'opengl');
set(get(gca,'child'), 'FaceColor', 'interp', 'CDataMode', 'auto');
view(2)
axis equal
axis([478 522 478 522])
grid minor
xlabel('x')
ylabel('y')
This example produces something like this:
Note, that this is a re-binning of your data so your output may look slightly different compared to before. The bins have been shifted to align their centers and the histogram recalculated.

SLicing a 3-d plot in MATLAB

Suppose in MATLAB I have obtained a 3-D plot, like surf(peaks(20)) how do I get a slice along the plane X=0, and the corresponding 2-D plot?
You can make a contour plot:
data = peaks(50);
figure;
surf(data);
figure;
[C,h] = contour(data, [0 0]);
clabel(C,h);