Aligning histogram plots - matlab

I have a given data set, and I want to compare the histograms of this data when represented as a bar histogram and a line histogram. Specifically, I want to use
myhist = histogram(mydata)
to get the bar histogram, and plot on the same figure a line histogram using
mylinehist = plot(myhist.Values)
However, when I do that, I get the following figure
It seems like the line histogram mimics the shape of the bar histogram, but offsets it by a certain amount on the x-axis. Is there a way to align the two so I can have them overlapping? I tried using a command like
align([mylinelist,myhist],'Left','None')
but to no avail. Thanks!

You need to specify the x-axis values for your line plot. These should be the midpoints of your histogram bins.
Try:
midpts = myhist.BinEdges + (myhist.BinWidth / 2);
plot(midpts(1:myhist.NumBins), myhist.Values);

Related

Matlab histcounts show values on x-axis

I want to draw a histogram, using plot(histcounts(X,edges)).
It works fine, except that on the x-axis, the number of the bin is displayed, not the actual value the bin refers to.
To make a bit clearer what I mean, I append two plots. Both display the same data, but for the first one, I used plot(histcounts(X,edges)) and for the second hist(X,edges). The plot for which I used hist shows the x-axis the way I want it to look like, with the value the bin refers to. I would like the plot(histcount(...) to have the same x-axis, instead of showing the bin number.
Histogram using plot(histcounts):
Histogram using hist:
How can I change the x-axis to show this value instead of the bin number?
Thanks a lot!
If you have the edges, you can get the centres using
centres = edges(1:end-1)+ diff(edges)/2;
then the plot can be
plot(centres, histcounts(X,edges));
If you do not need to specify the edges you can get them using
[h_counts, edges] = histcounts(X);

Matlab 2016a - how to get xTickLabels on only some of the xTicks on an imagesc?

I'm trying to create a 24x366 heatmap using imagesc with the x-axis labelled at 13 evenly spaced points as {'jan 15','feb 15',...,'dec 15','jan 16'}, and the y-axis labelled at every row from 1 to 24, like this:
Desired imagesc axes
When I run the script, it displays the y-axis as I want it, but it only displays the first label on the x axis and ignores the others. I can get this to work for a plot, but I can't get it to work for an imagesc. I've included my script below. Does anyone know how to make the imagesc display all 13 labels on the x-axis at evenly spaced intervals?
mylabels = {'jan 15','feb 15','mar 15','apr 15','may 15','jun 15','jul 15','aug 15','sep 15','oct 15','nov 15','dec 15','jan 16'};
testspacing = (1:(60*24*30):528480);
figure
imagesc(rand(24,366))
set(gca,'XTick',testspacing,'XTickLabel',mylabels,'XTickLabelRotation',45,'YTick',1:24,'YTickLabel',1:24)
The problem with your code is that your only one of your ticks from testspacing falls in the range of the plot testspacing=[1 43201 ...]. You can check the range of your x-axis by running xlim without any arguments.
You can rescale testspacing to fit your x-axis e.g. like:
xmax = 366;
mylabels = {'jan 15','feb 15','mar 15','apr 15','may 15','jun 15','jul 15','aug 15','sep 15','oct 15','nov 15','dec 15','jan 16'};
testspacing = (1:(60*24*30):528480);
testspacing = testspacing/max(testspacing)*xmax;
figure
imagesc(rand(24,xmax))
set(gca,'XTick',testspacing,'XTickLabel',mylabels,'XTickLabelRotation',45,'YTick',1:24,'YTickLabel',1:24)
or you just generate testspacing properly. Since you are putting own labels on the axis anyway you might just choose use testspacing = [0:30.5:366] or testspacing = [0:30:366] depending on what you want. This will also help you debug your own code later on.
On another note you should think about reducing the number of labels in general and decide which of the labels are really helpful. Maybe every 2nd or 3rd month is enough. You can "remove" individual labels by setting them to empty strings ''.

Matlab: Format the decimals in contour labels

I want to cut the number of decimals in the following contour plot. I do:
[cc,hh] = contour(X,Y,Z,levels,'LineColor','k');hold on
texth = clabel(cc,hh,'FontSize',8);
which gets me the first contour with long labels. Then in order to cut the decimals I do:
for i = 1:size(texth); textstr=get(texth(i),'String'); textnum=str2double(textstr); textstrnew=sprintf('%0.0f', textnum) ; set(texth(i),'String',textstrnew); end
And this gives the second plot. As you see, there is a wide gap between the label and the contour lines which looks awful. Any ideas how to solve this?
Instead of modifying the result, create a contour plot with the levels you want, so you don't need to cheat the data.
Define levels as e.g. levels=997:1010
and then
contour(X,Y,Z,levels,'LineColor','k','ShowText','on');
Will create a contour plot with the text included and the levels being specifically the ones in the variable levels, in this case 997,998,999,...,1009,1010
If, as #David suggests, your levels variable already is a vector, then replace it by round(levels) as himself suggested.

plotting a text file with 4 columns in matlab

I want to plot a text file with 4 columns that first column in longitude,second in latitude, third is depth and forth is amount of displacement in each point.(it's related to a fualt)
-114.903874 41.207504 1.446784 2.323745
I want a plot to show the amount of displacement in each point (like images that we plot with imagesc),unfortunately "imagesc" command doesn't work for it.
how can I plot it?
Thanks for your attention
A simple way would be to use scatter3 and assign your displacements to be the colours. Note that you have to supply a size for this to work - I'm using [] (empty matrix) which will set it to default. If your four sets of values are four vectors of the same size, then it's just something like:
scatter3(lat,lon,depth,[],displacement, 'filled')
Values in displacement will be linearly mapped to the current colormap. 'filled' gives you filled markers rather than open ones (default marker is a circle but can be changed).
You can plot each point using plot3(longitude,latitude,depth). You can color each point according to the displacement in a for loop. The easiest way to do this is create a colormap, e.g. using jet and chosing the color according to the displacement.
figure;
hold on;
cmap = jet(256);
dispRange = [min(displacement),max(displacement)];
for k=1:size(longitude,2)
c = cmap(1+round(size(cmap,1)*(displacement(k)-dispRange(1))/dispRange(2)),:);
plot3(longitude(k),latitude(k),depth(k),'o', ...
'MarkerEdgeColor',c,'MarkerFaceColor',c);
end

Change histogram to curve in Matlab but not "fit"

I am using matlab to process data to get a radius distribution function. Now I get data(an array) of different distances of other atoms to 1 specific atom.
I used the "hist" command (hist(radius1,400)) and get a histogram:
But what I want is a curve, like this:
http://upload.wikimedia.org/wikipedia/commons/3/31/Lennard-Jones_Radial_Distribution_Function.svg
I tried some fit command, but it would give me a normal-distribution-like curve, which is not what I want. actually no fit is fine, I only want a curve to show its varying.
The raw data was a 4000*1 array of radius, is there any other way to get a curve of the top of each bar of the histogram?
Thanks so much.
Instead of automatically plotting a histogram using hist, you can get it to output the values:
[x, c] = hist(radius1,400);
x is the data in each bin, c the centre of each bin, so this replicates a histogram and then overplots a line on it (which will just connect the top of each bar so it may not look as smooth as you hoped):
bar(c,x);
hold on
plot(c,x,'r');
It is possible to use fit with an anonymous function as a custom model, but that may be overkill in this situation.