在MATLAB中用alpha值绘制圆 [英] Plot circles with alpha values in MATLAB

查看:130
本文介绍了在MATLAB中用alpha值绘制圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像您一样创建一些数据的散点图,并且我有许多重复的数据点,我希望将其绘制为具有某些alpha值的圆,以便在同一点上叠加更多的点位置很明显.

I'm creating a scatter plot of some data, as you do, and I have a number of repeated data points which I'd like to plot as circles with some alpha value so that piling on of extra points at the same location is obvious.

据我所知,您无法设置使用plot(x, y, 'o')生成的小圆圈的alpha属性,因此我求助于自己使用patch()绘制数千个小圆圈:

As far as I can tell you cannot set the alpha properties of the little circles that you generate with plot(x, y, 'o'), so I've resorted to drawing thousands of little circles using patch() myself:

x = repmat([1:10], [1 10]);
y = round(10*rand(100, 1))/10;
xlim([0 11])
ylim([0 1])
p = ag_plot_little_circles(x', y, 10, [1 0 .4], 0.2);

function p = ag_plot_little_circles(x, y, circle, col, alpha)
%AG_PLOT_LITTLE_CIRCLES Plot circular circles of relative size circle
% Returns handles to all patches plotted

    % aspect is width / height
    fPos = get(gcf, 'Position');
    % need width, height in data values
    xl = xlim();
    yl = ylim();
    w = circle*(xl(2)-xl(1))/fPos(3);
    h = circle*(yl(2)-yl(1))/fPos(4);

    theta = 0:pi/5:2*pi;
    mx = w*sin(theta);
    my = h*cos(theta);
    num = 0;
    for k = 1:max(size(x))
        for f = 1:size(y,2)
            num = num+1;
            p(num) = patch(x(k)+mx, y(k,f)+my, col, 'FaceColor', col, 'FaceAlpha', alpha, 'EdgeColor', 'none');
        end
    end
end

如您所见,这并不是最佳选择,因为我需要在绘制图之前知道并设置图的大小(xlimylim),以使圆最终变为圆形.如果我改变情节的形状,那么它们最终会变成椭圆形.我最终还拥有数百万个对象,这在创建图例时很痛苦.

As you can see, this is not optimal as I need to know and set the size of the plot (xlim and ylim) before I plot it so that the circles end up being circular. If I reshape the plot then they end up as ovals. I also end up with millions of objects, which is a pain when making legends.

有没有更简单的方法?

推荐答案

在Matlab Exchange上找到了patchline函数,该函数可以接受任何patch自变量,包括'FaceAlpha':

Found the patchline function on the Matlab Exchange that can take any patch arguments including 'FaceAlpha':

http://www.mathworks.com/matlabcentral/fileexchange/36953-patchline/content/patchline.m

这篇关于在MATLAB中用alpha值绘制圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆