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)
Related
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);
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 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.
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.
I have 3 vectors of data: latitudes, longitudes, and elevations of specific locations. Each vector (lat, lon, elev) is a column vector with about 63 elements where element 1 of each represents a given location. What I want to do is create a topological map, or heatmap (whichever you want to call it) to map out these locations. I can plot them (like lon vs lat) no problem, but I'm not sure where to look to create a topological map. I've looked at using the surf function, but the elevations need to be a 2-D matrix for that and that would result in a lot of missing data that I just don't have.
Can someone give me some guidance here?
You can use TriScatteredInterp to interpolate your data onto a regular grid, which you then can use to plot the surface using surf, or a heatmap using contourf.
The example in the linked documentation of TriScatteredInterp will do exactly what you need, all you need to do is replace x, y, and z by your data and define the appropriate limits for meshgrid.