通过颜色渐变修补圆形

时间:2019-04-10 14:57:26

标签: matlab plot matlab-figure

我试图绘制一个我希望沿轴均匀的颜色渐变(在下面的图片中,角度为pi/7定义)

当我使用patch命令时,该图与所需的梯度方向匹配,但沿其方向并不均匀(沿圆弧的点之间形成了各种三角形)

enter image description here

这是代码

N=120;
theta = linspace(-pi,pi,N+1);
theta = theta(1:end-1);
c = exp(-6*cos(theta-pi/7));
figure(1)
patch(cos(theta),sin(theta),c)
ylabel('y'); xlabel('x')
axis equal

1 个答案:

答案 0 :(得分:5)

您必须定义Faces属性,以确保颜色填充垂直于轴的条纹(请参见Specifying Faces and Vertices)。否则,MATLAB将使用某种算法来平滑地混合颜色,如其所见。

N=120;
a = pi/7;

theta = linspace(a,2*pi+a,N+1); % note that I changed the point sequence, this is just to make it easier to produce the matrix for Faces.
theta(end) = [];

ids = (1:N/2)';
faces = [ids, ids+1, N-ids, N-ids+1];

c = exp(-6*cos(a-theta))';

figure
patch('Faces', faces, 'Vertices',[cos(theta);sin(theta)]','FaceVertexCData',c, 'FaceColor', 'interp', 'EdgeColor', 'none')
ylabel('y'); xlabel('x')
axis equal

enter image description here