I am trying to plot a dataset using imagesc in Matlab.
The dataset is structured like this:
x1 y1 value1
x2 y2 value2
x3 y3 value3
...
The problem:
when I try to plot it like this:
imagesc(x,y,value)
the figure is only in one dimension.
It works well when I plot it with plot3, using the values for the z-axis.
How can I visualize this dataset using imagesc?
imagesc needs a matrix structure rather than the 3 vector you mentioned, and assumes that the data is used in uniform space grids. So I'd use scatter instead to begin with. A way to still use imagesc is to interpolate to an uniform grid and construct a matrix out of the 3 vectors you have:
If you want to convert your non-uniform data the function you are looking for is griddata.
It handles the interpolation and returns a matrix of values.
This can be plotted by imagesc, surf or whatever.
scatter is usually the better way, but that depends on your application.
Try looking the source code of imagesc function. You can see how it is made. To see it, write:
edit imagesc
Related
I'm doing a project that involves making a 3D model of the cornea in matlab. I have 6 plot3 in the same graph to draw one cornea
but now i want a surface plot.
Don't mind the curve orientation.
Note that all the plot3 have x, y and z that are vectors
Thanks in advance
If I were you I would use the Surf command doku surf. It is used to display [x,y,z] data. Since you have not have as many touples of data (just 6) you will have to interpolate all the other values. Therefore I would use the scattered interpolant function doku scattered interpolant.
!!!!!!!!!!!!!!Take care all this is pseudocode!!!!!!!!!!!!!!!!
F = scatteredInterpolant(x_existing,y_existing,z_existing);
generates a scattered interpolant object. You do already feed your already existing data in there. Afterwards you generate the points at which you want to interpolate:
%generates samples from -4 t0 4 in 0.05 steps
[x_sample,y_sample] = meshgrid(-4:0.05:4,-4:0.05:4);
Now you calculate the fitted z values using the scattered interpolant obj
z_interpolated=F(x_sample,y_sample) %interpolates
surf(x_sample,y_sample,z_interpolated) %plots with surf between -4 and 4
!!!!!!!!!!!!!!!From here working code!!!!!!!!!!!!!!!!!!!!!
%serialiasation of data (special for this usecase)
x_data=[h0(30:632,6);(a30(28:408,3))+0.527;(a60(276:632,3));(a90(26:575,3))+3.417;(a120(188:586,3))-0.6625;(a150(16:380,3))+1.173];
y_data=[(h0(30:632,5));((a30(28:408,2))-0.9128);(a60(276:632,2));(a90(26:575,2));(a120(188:586,2))-0.3825;((a150(16:380,2))+2.032)];
z_data=[yA0;yA30+0.162;yA60;yA90+0.837;yA120+0.135;yA150+0.135];
% cleaning the data of nan values
x_data=x_data(~isnan(z_data));
y_data=y_data(~isnan(z_data));
z_data=z_data(~isnan(z_data));%random for the looks
%interpolating
F=scatteredInterpolant(x_data,y_data,z_data);
%read yourself what this does
F.Method = 'natural';
F.ExtrapolationMethod = 'none';
%choosing sample points
[x_sample,y_sample] = meshgrid(-6:0.05:6,-6:0.05:6);
%interpolation
z_interpolated=F(x_sample,y_sample);
%plot
surf(x_sample,y_sample,z_interpolated)
I hope I was able to help you. If you try it and it works it would be very nice of you to post the working code here so that in the future here stands a working solution.
I have a 100 x 100 matrix and i have to use plot3 in MATLAB environment to graph this data. I tried plot3(matrix name) but I faced this error "not enough input arguments". I think plot3 needs 3 input arguments, but I only have this matrix of data. could anyone help me to solve this problem? Is there any alternative for plot3 when we don't have enough arguments?
I need a graph like this:
I think you want to plot the values in a figure as a sort of surface element. What you can do then is:
[X,Y] = size(matrix);
figure;
surface(1:X,1:Y,matrix);
What this does is that it creates a vector for both X and Y indices, as possible in surface. The X and Y indices are obtained by setting them as integers from 1:size, so basically you assign the location of each matrix element to an index.
Note that you can strictly speaking use surface(matrix) as well, but the former approach allows you to use custom indexing, as long as the lengths of the vectors X and Y are the same as the size of your matrix.
For the waterfall use:
figure;
waterfall(matrix);
Sample code:
A=rand(100);
figure;
waterfall(1:100,1:100,A);
Gives:
where you can play around with the name-value pairs, see the documentation on that.
I think what you need is mesh or surf instead of plot3.
plot3 draws a line in 3d-space, so it will need three vectors of the same length (one for each dimension).
When you have a matrix, one reasonable way of displaying it is as a surface in 3d space, which is done by the functions mesh and surf.
Try it out! I hope i helps!
I've been using interp1 to plot curves to follow sets of datapoints, and for most of the datapoints it's been working:
But when I try it with another set of datapoints it doesn't follow them at all:
For both interpolations the code I'm using is just:
curve = interp1(x, y, 'pchip');
Where x is just a set of numbers that correspond to the x axis of each datapoint, and y is the values themselves.
I can't tell what is different about the second dataset that is causing the interp1 function to not follow the data.
So with thanks to #m.s. for providing his code, it turns out the issue is that with the second graph I was interpolating with x= -90:10:90, whereas if I interpolate with 1:19, in a similar manner to the first graph, then the problem is fixed.
I would like to plot data set 1 and data set 2 in one plot vertical. Unfortunately the data is huge, so it is just a smear of points and can't see the density. I tried hist3 and other suggestions but it overwrites my data sets and the binning looks awful.
Is there another way to plot scatter density plots? Is there really no Matlab function for it? If not, which program could I use to easy generate such a plot?
A mix between this two examples:
(source: bcgsc.ca)
Thanks to #Emil Albert for a correction (a transpose was missing)
What's wrong with computing hist3 and displaying the result with imagesc?
data1 = randn(1,1e5); %// example data
data2 = randn(1,1e5) + .5*data1 ; %// example data correlated to above
values = hist3([data1(:) data2(:)],[51 51]);
imagesc(values.')
colorbar
axis equal
axis xy
If you want to have the axes in accordance with the true data values: use the second output of hist3 to obtain the positions of the bin centers, and pass them to imagesc:
data1 = randn(1,1e5); %// example data
data2 = 2*randn(1,1e5) + 1.2*data1 + 4; %// example data correlated to above
[values, centers] = hist3([data1(:) data2(:)],[51 51]);
imagesc(centers{:}, values.')
colorbar
axis xy
Try Violin Plot submission on File Exchange. It's very customizable. I use it all the time. Thanks to #Jonas.
I'm pretty new to Matlab and I would like to make a plot with errorbars. I have the errors in a vector expressed in % of the measured values. I have tried to use Matlab's errorbar but it is only shifting the plot in a strange way.
Standard usage is errorbar(x,y,yerr, ...options...).
In your case sounds like yerr = y.*percenterr/100