Creating a Bar Graph where each Bar is a Histogram - matlab

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.

Related

How to plot overlapping images with matlab

I have several dataset matrices x, y, andz, where z contains values at the positions x,y showing shifted (overlapping) parts of the same picture. x and y are rectangularly centered around different center positions for each dataset.
How can I combine the data in one plot using pcolor or similar? Note that it should be a rectangular plot in the end, but that not all data points are given due to the shift.
I now solved my own question by using the command hold on, which makes it possible to plot several times into the same figure. You just have to run it in between two plot commands.

MATLAB: Digitizing a plot with multiple variables and implementing the data

I have 8 plots which I want to implement in my Matlab code. These plots originate from several research papers, hence, I need to digitize them first in order to be able to use them.
An example of a plot is shown below:
This is basically a surface plot with three different variables. I know how to digitize a regular plot with just X and Y coordinates. However, how would one digitize a graph like this? I am quite unsure, hence, the question.
Also, If I would be able to obtain the data from this plot. How would you be able to utilize it in your code? Maybe with some interpolation and extrapolation between the given data points?
Any tips regarding this topic are welcome.
Thanks in advance
Here is what I would suggest:
Read the image in Matlab using imread.
Manually find the pixel position of the left bottom corner and the upper right corner
Using these pixels values and the real numerical value, it is simple to determine the x and y value of every pixel. I suggest you use meshgrid.
Knowing that the curves are in black, then remove every non-black pixel from the image, which leaves you only with the curves and the numbers.
Then use the function bwareaopen to remove the small objects (the numbers). Don't forget to invert the image to remove the black instead of the white.
Finally, by using point #3 and the result of point #6, you can manually extract the data of the graph. It won't be easy, but it will be feasible.
You will need the data for the three variables in order to create a plot in Matlab, which you can get either from the previous research or by estimating and interpolating values from the plot. Once you get the data though, there are two functions that you can use to make surface plots, surface and surf, surf is pretty much the same as surface but includes shading.
For interpolation and extrapolation it sounds like you might want to check out 2D interpolation, interp2. The interp2 function can also do extrapolation as well.
You should read the documentation for these functions and then post back with specific problems if you have any.

In MATLAB, how can I combine 2 different `histc` results into one, and produce a 3D plot?

In one histc, I have latitude data. In another histc, I have longitude data. I have plotted the bar graphs of these 2 separately. Now, I want to combine them and produce a 3D graph where the x-axis is latitude, y-axis is longitude, and z-axis is the frequency with which each latitude-longitude pair occurs.
The problem is that, while plotting the graphs for latitudes and longitudes separately, I calculated their respective frequencies by taking their individual histcs separately. However, when I want to make it a 3D plot, I can't seem to find a way to take the histc of the latitude-longitude pairs.
EDIT: I am adding my code for plotting the bar graphs here upon being asked by one commenter to do so, though I don't see how that would help. The bar graph for latitude is: bar(unique(M), histc(M,unique(M))) and that for longitude is bar(unique(N), histc(N,unique(N))). And the M and N are nx1 matrices. (Actually, they are 2 columns of a much larger matrix. But for simplicity in comprehension, I have avoided writing complex formulae here.)
EDIT: I reckon what I am looking for might be solved by surface plot, surf. But I am not sure. If it is, then the issue I am facing, speaking in terms of surf, can be stated as an issue in defining the Z parameter.
latitudes=180*(rand(1,10000)-0.5);
longitudes=360*(rand(1,10000)-0.5);
d=[latitudes;longitudes];
minVal_Lat=-90;
maxVal_Lat=90;
minVal_Long=-180;
maxVal_Long=180;
delta=10;
axisLat=minVal_Lat:delta:maxVal_Lat;
axisLong=minVal_Long:delta:maxVal_Long;
nPDF_Lat=length(axisLat);
nPDF_Long=length(axisLong);
PDF=zeros(nPDF_Lat,nPDF_Long);
temp=0;
count_i=1;
count_j=1;
for i=axisLat;
lowlimit_x=i-delta/2;
upperlimit_x =i+delta/2;
for j=axisLong;
lowlimit_y=j-delta/2;
upperlimit_y =j+delta/2;
temp=0;
for k=1:length(d(1,:));
if lowlimit_x<=d(1,k) & d(1,k)<upperlimit_x
if lowlimit_y<=d(2,k) & d(2,k)<upperlimit_y
temp=temp+1;
else
end
else
end
end
PDF(count_i,count_j)=temp;
count_j=count_j+1;
end
count_i=count_i+1;
count_j=1;
end
normFactor=sum(sum(PDF));
PDF=(1/normFactor)*PDF;
randVar_Lat=minVal_Lat:delta:maxVal_Lat;
randVar_Long=minVal_Long:delta:maxVal_Long;
surf(randVar_Lat,randVar_Long,PDF')
If you want get a 2D probability density function with surf plot, these codes will be worked.

Contouring a mesh and assigning magnitude arrows in Matlab

I want to assign vector to a contourf graph, in order to show the direction and magnitude of wind.
For this I am using contourf(A) and quiver(x,y), where as A is a matrix 151x401 and x,y are matrices with the same sizes (151x401) with magnitude and direction respectively.
When I am using large maps i get the position of the arrows but they are to densily placed and that makes the graph look bad.
The final graph has the arrows as desired, but they are to many of them and too close, I would like them to be more scarce and distributed with more gap between them, so as to be able to increase their length and at the same time have the components of the contour map visible.
Can anyone help , any pointers would be helpful
i know its been a long time since the question was asked, but i think i found a way to make it work.
I attach the code in case someone encounters the same issues
[nx,ny]= size(A) % A is the matrix used as base
xx=1:1:ny; % set the x-axis to be equal to the y
yy=1:1:nx; % set the y-axis to be equal to the x
contourf(xx,yy,A)
hold on, delta = 8; %delta is the distance between arrows)
quiver(xx(1:delta:end),yy(1:delta:end),B(1:delta:end,1:delta:end),C(1:delta:end,1:delta:end),1) % the 1 at the end is the size of the arrows
set(gca,'fontsize',12);, hold off
A,B,C are the corresponding matrices ones want to use

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.