Cirle with color changing gradually.

How can I plot a circle like this in matlab, please give me an example. Thanks in advance.

 采纳的回答

arich82
arich82 2015-7-17
编辑:arich82 2015-7-17
[Edited to add colorbar and image.]
As far as I know, you can't do it directly, but pol2cart is your friend. Look at
and maybe
% construct grid of polar coords
th = (0:5:360)*pi/180;
r = 0:.05:1;
[TH, R] = meshgrid(th, r);
% convert polar to cartesian
[X, Y] = pol2cart(TH, R);
% dummy function plotted over X Y data
Z = X + 1i*Y;
f = abs((Z.^4 - 1).^(1/4));
% use overhead view of surf plot for figure
figure;
surf(X, Y, f);
view(2);
axis equal;
axis square;
colorbar('SouthOutside');
colormap('Jet');
Note that axis(2) gives an overhead view, so it looks like a flat circle, and axis equal ensures it looks like a circle and not a distorted ellipse.
Please accept this answer if it helps, or let me know what's wrong in the comments.

6 个评论

Thank you so much, but the problem here is Plot is in polar co-ordinate. You can imagine like this, every point in the circle has a value which is defined by a function f(radius, angle) (this function f is implicit), so that you only know the value of f but not the expression of f. Now we have value of each point in the cirle and its coordinate (radius and angle), how can I solve then. Thank you.
The above works for precisely this case, assuming you have data on a regular grid of (r, th) coordinates. If not, you'll probably have to interpolate.
Do you have data sampled at, e.g., theta every 5deg th = (0:5:360)*pi/180 and along the radius at every 5cm r = (0:0.05:1)? If you do, that's what's plotted above (you could trivially change the grid spacing for theta on a 1deg spacing via th=(0:1:360)*pi/180, and likewise for r).
If your data is sampled at random (r, th) pairs (i.e. a scatter plot), then the best method for interpolation will depend on both the version of Matlab you're using, and the nature of your data. I'll use scatteredInterpolant with the natural method in Matlab 2014a as an example.
I assume you already have values for r, th, and f, so the following is just dummy data for my example:
% dummy data sampled randomly
rng('default');
n = (72+1)*(20+1)
th = rand(n, 1)*2*pi;
r = rand(n, 1);
[x, y] = pol2cart(th, r);
f = abs(((x + 1i*y).^4 - 1).^(1/4)); % just dummy data; let f be your real data
figure;
scatter3(x, y, f, 10, f, 'filled');
view(2)
axis equal;
axis square;
colorbar('SouthOutside');
colormap('Jet');
caxis([0, 1.189207115002721]); % colorbar limits from previous plot, for comparison
title('scatter plot of randomly sampled data')
We'll proceed as before generating a regular grid in r--|th| space, but use our vector of randomly sampled data to create an interpolating object. We'll interpolate on our regular grid, convert the grid to cartesian using pol2cart as before, and plot as before. (So the only extra step is creating the interpolant object, then interpolating.):
[TH, R] = meshgrid((0:5:360)*pi/180, 0:0.05:1);
f_interp = scatteredInterpolant(r, th, f, 'natural');
F = f_interp(R, TH);
[X, Y] = pol2cart(TH, R);
figure;
surf(X, Y, F);%, 'EdgeColor', 'none');
view(2);
axis equal;
axis square;
colorbar('SouthOutside');
colormap('Jet');
caxis([0, 1.189207115002721]); % colorbar limits from previous plot, for comparison
title('reconstructed data');
Note that, in the scattered plot, there was a sparsely sampled region in the 3-o'clock position, and in the reconstructed plot, there's a bit of a yellow artifact in the same place, when compared to the original plot shown in the answer above.
Does this help?
Thank you, another question is that I have r = linspace(0,90,20); theta = linspace(0,360,10); f = rand(length(r), length(theta)); How can I plot in this situation, r is radius, and theta is angle from 0 to 360 degree
This is exactly the case plotted in the original answer, where you have data on a regular grid. The only tricky thing is to make sure you convert your theta from degrees to radians before the input to pol2cart:
% construct grid of polar coords
th = linspace(0,360,10)*pi/180;
r = linspace(0,90,20);
[TH, R] = meshgrid(th, r);
% convert polar to cartesian
[X, Y] = pol2cart(TH, R);
% dummy data
rng(0);
f = rand(length(r), length(th));
% use overhead view of surf plot for figure
figure;
surf(X, Y, f);
view(2);
axis equal;
axis square;
colorbar('SouthOutside');
colormap('Jet');
Is there something unclear about how to modify the code from the original answer?
Thank you so much. It worked! :)
Unfortunately, surf() is not compatible with the new-ish polaraxes (R2016a and later).
You could use the older polar() to create the basic background graphic, and then "hold on", and then plot on top of it.

请先登录,再进行评论。

更多回答(1 个)

The File Exchange contribution https://www.mathworks.com/matlabcentral/fileexchange/17933-polar-to-from-rectangular-transform-of-images transforms rectangular images to and from polar to achieve the effect this Question is about.

类别

标签

Community Treasure Hunt

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

Start Hunting!

Translated by