What is the best way to create pcolor plot in the case when 1d-arrays given?

Hello community. Could you provide an example for pcolor plot, if my dataset is described like this:
x=getSomeX(); % returns 1-by-n vector, in general just random doubles without any sequence
y=getSomeY(x); % returns 1-by-n vector
z=getSomeZ(x); % returns 1-by-n vector
where n is constant length, and i want to do something like this
pcolor(x,y,z);
but for me the problem is to make a correct n-by-n matrix z.
Thanks!

 Accepted Answer

You cannot directly create pcolor plots in that situation. Instead see griddedInterpolant() https://www.mathworks.com/help/matlab/ref/griddedinterpolant.html or scatteredInterpolant() . Once you have the interpolated data, you can pcolor() that.

3 Comments

Hello! Thanks for reply, but it is not a solution because griddedinterpolant(x,y,z) requires following dimensions for args: x=1-by-n, y=n-by-1, z=n-by-n. So it looks like the same problem. In my dataset y and z depends only on x and i want to plot points in x and y coords, and then apply a color defined by z value.
PS solution is something like this
also
https://www.mathworks.com/matlabcentral/fileexchange/21617-vec2grid
https://wspr.wordpress.com/2015/05/29/surf-plotting-scattered-data-in-matlab-or-delaunay-interpolation-without-a-grid/
Then use scatteredInterpolant instead of griddedInterpolant.
scatteredInterpolant is OK, found an example here
https://blogs.mathworks.com/graphics/2016/02/24/on-the-grid/
npts = 250;
rng default
x = 2*randn(npts,1);
y = 2*randn(npts,1);
v = sin(x) .* sin(y);
figure; scatter(x,y,36,v,'filled')
colorbar
xlim([-2*pi 2*pi])
ylim([-2*pi 2*pi])
[xg,yg] = meshgrid(linspace(-2*pi,2*pi,125));
F = scatteredInterpolant(x,y,v);
vg = F(xg,yg);
figure; pcolor(xg,yg,vg);
shading flat;
Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!