Matlab histcounts show values on x-axis - matlab

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);

Related

How to rescale vector field onto a different grid?

I currently have a vector field that looks something like this, generated with the following basic structure, where Z is some matrix:
[X,Y] = meshgrid(x,y)
[grad_x, grad_y] = gradient(Z)
quiver(X,Y,grad_x,grad_y)
I would like for this plot to be rescaled, such that the x-axis ranges from 1.5 to 3.8 and the y-axis ranges from 100 to 250, but for the arrows themselves to look identical. The only difference in the figure should be the axes labels.
I have tried:
grad_x_rescaled = [(grad_x - min(grad_x))./(max(grad_x)-min(grad_x))].*(3.8-1.5);
grad_y_rescaled = [(grad_y - min(grad_y))./(max(grad_y)-min(grad_y))].*(250-100);
But the problem with this is that although the grad_x and grad_y get rescaled overall, the scaling of the arrows themselves relative to each other are not conserved, and results in below (note the thick black streaks are presumably arrowheads, but the important thing is that the direction and relative sizes of the arrows are not exactly like in the first case.
Is there a matlab function or an expression to renormalize data into a new range, but such that the renormalized data is scaled relative to itself (such as the arrows should be scaled the same relative to one another)?
To simply change the axes tick labels you could use Matlab's ability to specify tick marks and tick labels. Basically you would just tell Matlab where to put the ticks and what the labels should say like this:
xticks(linspace(0,1,6))
xticklabels(linspace(1.5,3.8,6))
yticks(linspace(0,1,6))
yticklabels(linspace(100,250,6))

Aligning histogram plots

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);

Creating a Bar Graph where each Bar is a Histogram

I'm creating a line plot, where the y value of each point is the average value of vector i. The x value of each point is i.
I want to visualise the distribution of numbers in each vector, preferably all on the same graph.
Is there a way I can make a bar graph, where each bar, i, is something like a colorbar, representing the histogram of vector i. So essentially I want to end up with 20 or so bars, each being a histogram.
Or if there is a better way to visualise numerous histograms on a single plot, I'd like to hear it.
I solved the problem using Dan's solution. I took a histogram of each vector (with specific bin intervals), and stored them all in a 2D matrix (Each column is a complete histogram). Then displayed it with image() (Don't have access to imshow).
I did have to mess around with the axis labels though, as the image() function was plotting it according to the coordinates of the 2D matrix, rather than the values in the original vectors. Fixed that up with some calls to set(gca,'YTickLabel/YTick'). Also had to set the YDir back to 'normal' rather than 'reverse'. I think image() was flipping it.

Ignoring non-specified points in matlab plot

how can we ignore the non-specified values in x-axis label of a matlab plot?
for ex:
if my
x=[201:210];,y=rand(size(x));
I would like to display only those specified values of x such as 201,202,203..instead of with the intermediate values such as 201.5,202.5.. Basically I wanna get rid of these decimal values in my plot.
thanks in advance.
DURAI
If you don't want to plot them. You can just specify your variables used in plot.
plot(x(1:2:end),y(1:2:end))
this would plot only each second value. Obviously you can use logical indexing and any other means e.g. another index-array as well.
plot(x(x>10),y(x>10))
as another example. Just take care that you use the same commands in both variables otherwise you will get wrong results or an error (if number of points don't match up).
If you want to plot specific values you can use a for loop to loop over the values you want to use:
x=1:1:10;
y=115:15:250;
figure(2)
for x=[4,5,7]
display(x)
plot(x,y(x),'x'); hold on
end
If you want to just change the area which is displayed but use the whole set of data points you can use:
axis([xmin,xmax,ymin,ymax])
or if you want to change the labels of x-axis use:
set(gca, 'XTick',new_x_axis_steps, 'XTickLabel',new_x_labels(new_x_axis_steps))
where new_x_axis_steps is an array which defines the start, end point and the step and new_x_labels is what you want to write there (if you want to use strings), otherwise just use:
set(gca, 'XTick',new_x_axis_steps)

Set Matlab's Contour graph x-axis scale?

I am trying to graph a contour graph with a two dimentional matrix, v. v contains velocity data, y-index represents depth, x-index is mapped to a 1d-vector containing latitude info, lat.
when I graph contour(v), the x-axis is index, but I wish to show the latitude (also scale accordingly), I tried contour(lat,v) but it just shows me a blank graph, how should I graph it?
Without having an example dataset, it's hard to say for sure... but I suspect that what you need to do is use the contour(X,Y,Z) form of contour. If you want to specify one axis, you need to specify both. In your case, it would be:
contour(lat,depth,v)
If you're happy using the y-index rather than an actual depth vector, you can do
contour(lat,1:size(v,2),v)