这是polarhistogram函数的MATLAB源代码:

function [hn,theta] = polarhistogram(x,nbins,varargin) %POLARHISTOGRAM Create polar histogram % POLARHISTOGRAM(X,NBINS) creates a histogram count of the angles in X, % splitting them into NBINS equally spaced bins. The angles are assumed % to be in radians. % % POLARHISTOGRAM(X,NBINS,Name,Value) specifies additional parameter name-value % pairs described below: % % 'BinLimits' - Two-element vector containing the lower and upper % limit of the histogram. Default is [0 2pi]. % 'BinEdges' - Vector of bin edges. Default is linspace(BinLimits(1),BinLimits(2),NBINS+1). % 'Normalization'- Normalization of the histogram. Options are: % 'count' (default), 'probability', 'countdensity', and 'pdf'. % 'DisplayStyle'- Display style of the histogram. Options are: % 'bar' (default), 'stairs', and 'line'. % 'EdgeColor' - Color of the edges of the histogram bars. Default is 'none'. % 'FaceColor' - Color of the interior of the histogram bars. Default is the ColorOrder of the axes. % 'LineWidth' - Width of the edges of the histogram bars. Default is 0.5. % 'FaceAlpha' - Transparency of the interior of the histogram bars. Default is 1. % 'AngleZero' - Angle in radians of the zero direction. Default is 0. % 'Axes' - Handle to the polar axes to plot into. By default, POLARHISTOGRAM plots % into the current axes. % % [HN,THETA] = POLARHISTOGRAM(...) returns the histogram counts in HN and the bin % centers in THETA. % % Example: % % Create a polar histogram of angles in a vector % x = rand(100,1)2pi; % polarhistogram(x,16); % % % Create a polar histogram with specified bin edges % figure % polarhistogram(x,linspace(0,2pi,17)); % % % Create a polar histogram with normalized counts % figure % polarhistogram(x,16,'Normalization','pdf'); % % % Create a polar histogram with a different zero angle % figure % polarhistogram(x,16,'AngleZero',pi/2); % % See also POLARAXES, POLARPLOT, HISTOGRAM, HISTOGRAM2D.

% Copyright 2016 The MathWorks, Inc.

% Parse inputs
[x,nbins,binEdges,binLimits,normalization,displayStyle,edgeColor,...
 faceColor,faceAlpha,angleZero,ax] = parseInputs(x,nbins,varargin{:});

% Bin the angles
binIdx = discretize(x,binEdges);

% Get the bin counts
hn = accumarray(binIdx,1,[nbins 1],[],0);

% Normalize the bin counts
switch normalization
    case 'count'
        % Do nothing
    case 'probability'
        hn = hn / numel(x);
    case 'countdensity'
        % Normalize by bin width
        binWidths = diff(binEdges);
        hn = hn ./ binWidths(:);
    case 'pdf'
        % Normalize both by bin width and total area
        binWidths = diff(binEdges);
        hn = hn ./ (binWidths(:) .* sum(hn));
end

% Create the polar plot
if isempty(ax)
    ax = newplot;
end
hold(ax,'on');
theta = [binEdges(1:end-1)+diff(binEdges)/2 binEdges(end)];
theta = mod(theta - angleZero,2*pi);
hn(end+1) = hn(1);
switch displayStyle
    case 'bar'
        h = polar(ax,theta,hn,'-');
        set(h,'Color',edgeColor,'LineWidth',1.5*get(h,'LineWidth'),...
            'Marker','none','MarkerFaceColor','none','MarkerEdgeColor','none');
        if ~strcmpi(faceColor,'none')
            h = polar(ax,theta,hn,'.');
            set(h,'Marker','none','MarkerFaceColor',faceColor,'MarkerEdgeColor',edgeColor);
            set(h,'MarkerSize',get(h,'MarkerSize')*5);
            set(h,'AlphaData',faceAlpha,'MarkerEdgeAlpha',faceAlpha,'MarkerFaceAlpha',faceAlpha);
        end
    case 'stairs'
        h = polar(ax,theta,hn,'-');
        set(h,'Color',edgeColor,'LineWidth',get(h,'LineWidth'),...
            'Marker','none','MarkerFaceColor','none','MarkerEdgeColor','none');
        if ~strcmpi(faceColor,'none')
            h = polar(ax,theta,hn,'.');
            set(h,'Marker','none','MarkerFaceColor',faceColor,'MarkerEdgeColor',edgeColor);
            set(h,'MarkerSize',get(h,'MarkerSize')*5);
            set(h,'AlphaData',faceAlpha,'MarkerEdgeAlpha',faceAlpha,'MarkerFaceAlpha',faceAlpha);
        end
    case 'line'
        h = polar(ax,theta,hn,'-');
        set(h,'Color',edgeColor,'LineWidth',get(h,'LineWidth'),...
            'Marker','o','MarkerFaceColor',faceColor,'MarkerEdgeColor',edgeColor,...
            'MarkerSize',get(h,'LineWidth')*5);
        set(h,'AlphaData',faceAlpha,'MarkerEdgeAlpha',faceAlpha,'MarkerFaceAlpha',faceAlpha);
end
hold(ax,'off');
view(ax,[-90 90]);
set(ax,'Visible','off');

end

function [x,nbins,binEdges,binLimits,normalization,displayStyle,edgeColor,... faceColor,faceAlpha,angleZero,ax] = parseInputs(x,nbins,varargin)

narginchk(2,inf);

% Parse required inputs
validateattributes(x,{'numeric'},{'real','vector','finite'},mfilename,'X',1);
validateattributes(nbins,{'numeric'},{'scalar','integer','positive'},mfilename,'NBINS',2);

% Parse parameter name/value pairs
parser = inputParser;
parser.FunctionName = mfilename;

% Bin limits
parser.addParameter('BinLimits',[0 2*pi],@(x)validateattributes(x,{'numeric'},{'real','vector','finite','numel',2},'','BinLimits'));
% Bin edges
parser.addParameter('BinEdges',[],@(x)validateattributes(x,{'numeric'},{'real','vector','finite'},'','BinEdges'));
% Normalization
parser.addParameter('Normalization','count',@(x)validateattributes(x,{'char'},{'row'},mfilename,'Normalization'));
% Display style
parser.addParameter('DisplayStyle','bar',@(x)validateattributes(x,{'char'},{'row'},mfilename,'DisplayStyle'));
% Edge color
parser.addParameter('EdgeColor','none',@(x)validateattributes(x,{'char'},{'row'},mfilename,'EdgeColor'));
% Face color
parser.addParameter('FaceColor',get(gca,'ColorOrder'),@(x)validateattributes(x,{'numeric','char'},{'row'},mfilename,'FaceColor'));
% Face alpha
parser.addParameter('FaceAlpha',1,@(x)validateattributes(x,{'numeric'},{'scalar','>=',0,'<=',1},mfilename,'FaceAlpha'));
% Line width
parser.addParameter('LineWidth',0.5,@(x)validateattributes(x,{'numeric'},{'scalar','positive'},mfilename,'LineWidth'));
% Angle zero
parser.addParameter('AngleZero',0,@(x)validateattributes(x,{'numeric'},{'scalar','real','finite'},mfilename,'AngleZero'));
% Axes handle
parser.addParameter('Axes',[],@(x)validateattributes(x,{'matlab.graphics.axis.AbstractPolarAxes'},{'scalar'},mfilename,'Axes'));

parser.parse(varargin{:});

% Assign outputs
binLimits = parser.Results.BinLimits;
normalization = validatestring(parser.Results.Normalization,{'count','probability','countdensity','pdf'});
displayStyle = validatestring(parser.Results.DisplayStyle,{'bar','stairs','line'});
edgeColor = parser.Results.EdgeColor;
faceColor = parser.Results.FaceColor;
faceAlpha = parser.Results.FaceAlpha;
angleZero = parser.Results.AngleZero;
ax = parser.Results.Axes;

% Bin edges
if isempty(parser.Results.BinEdges)
    binEdges = linspace(binLimits(1),binLimits(2),nbins+1);
else
    binEdges = parser.Results.BinEdges;
    nbins = numel(binEdges) - 1;
end

% Ensure the bin limits are consistent with the bin edges
assert(isequal(binEdges(1),binLimits(1)),'BinLimits(1) must match the first element of BinEdges.');
assert(isequal(binEdges(end),binLimits(2)),'BinLimits(2) must match the last element of BinEdges.');

end

用matlab定义polarhistogram函数源代码

原文地址: https://www.cveoy.top/t/topic/K9O 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录