电子产业一站式赋能平台

PCB联盟网

搜索
查看: 210|回复: 0
收起左侧

MATLAB|科研绘图|复刻风玫瑰图

[复制链接]

260

主题

260

帖子

1833

积分

三级会员

Rank: 3Rank: 3

积分
1833
发表于 2023-10-31 08:35:00 | 显示全部楼层 |阅读模式
点击上方蓝字和“好玩的MATLAB”一起快乐玩耍吧!
# o: D: ~; l5 ]0 W

u5tehliszzr64012791259.jpg

u5tehliszzr64012791259.jpg
3 ?2 M" W0 j& S- }! W+ W7 P. b
好玩的matlab
2 b4 _3 Q0 L  P! S: u带你解锁不一样的matlab新玩法
. z. H' h' t; y' ~5 y$ @; ?6 U+ J
半个月前有粉丝问我怎么画”风玫瑰图“,之前因为时间比较忙所以没有更新,今天有时间抽空介绍一下“风玫瑰图”的画法,喜欢此推文的小伙伴们记得点赞+关注+分享!【尊重作者劳动成果,转载请注明推文链接和公众号名】( _/ \! ?1 E4 r# x+ ^5 S
粉丝给的图:  S5 M6 p' x' n5 `4 ^

0fiem1gmqti64012791359.png

0fiem1gmqti64012791359.png
1 R; ?* S" q: u/ B) @
复刻的图:2 w  Q9 z9 U% V3 z7 R6 h

1ier3xoeixj64012791459.png

1ier3xoeixj64012791459.png
0 z$ R& Z: I0 U# K) N1 W* z* u1 |
其他样式效果:
( r2 G5 [) Q! D! j3 M( P, Q; |* t" r$ n

h4tau0shtog64012791500.gif

h4tau0shtog64012791500.gif

) j6 W& l# ^% A. X
+ [, N$ B8 y* I7 a0 z! a9 X, x- E& m5 @
数据数据包含:风向、刮风频率、最大风速、平均风速四个数据。
  • windDirections = [0.0, 22.5, 45.0, 67.5, 90.0, 112.5, 135.0, 157.5, 180.0, 202.5, 225.0, 247.5, 270.0, 292.5, 315.0, 337.5]';windFrequency = [10.2, 4, 4, 3.0, 4.0, 3.0, 4.0, 4.0, 7.0, 6.0, 3, 2, 2, 1.0, 3, 5]';maxWindSpeed = [15, 12, 10, 5, 5, 5,10, 12, 13, 15, 12,5, 5, 10, 12,  15]';avgWindSpeed = [2.8, 2.0, 1.5, 1.2, 1.4, 1.3, 1.45, 2.00, 2.80, 2.80, 1.50, 1.00, 1.0, 1.0, 1.8, 2.0]';data=[windDirections,windFrequency,maxWindSpeed,avgWindSpeed];数据长成这样的1 G& ?9 Q# A. G! m

    obfkxpin0gs64012791600.png

    obfkxpin0gs64012791600.png

    1 ^7 q# X1 L: J# _1 Y3 J
    6 C4 I) N7 U; W; M+ @1 o' \2 Y8 r% `绘图教程绘制左边Y轴
    2 V) U+ Q7 V! ~9 C, f9 _
  • % 设置基础参数maxRadius = ceil(max(windFrequency));minRadius = round(maxRadius / 10);dfT=unique(diff(windDirections));allTheta = 0:0.01:360;% 创建并配置坐标轴fig=figure('Color',[1,1,1]);ax = gca;hold on; box off; grid off; axis equal;ax.YLim = [-maxRadius, maxRadius];ax.YMinorTick = 'on';ax.XColor = 'none';ax.XDir = 'normal';ax.TickDir = 'out';ax.LineWidth = 1.5;majorTicksY = ax.YTick;minorTicksY = (majorTicksY(1:end-1) + majorTicksY(2:end)) / 2;diffTicks = unique(diff(majorTicksY));maxRadius=max(ax.YTick)+diffTicks/2;ax.YLim = [-maxRadius, maxRadius];%重新设定ax.YAxis.MinorTickValues = [minorTicksY(1)-diffTicks, minorTicksY, minorTicksY(end)+diffTicks];ax.YTickLabel = arrayfun(@(y) sprintf('%d', abs(y)), majorTicksY, 'UniformOutput', false);ax.YLabel.String='风向频率(%)';
    $ J" q8 J) x" |" Y1 ~* D9 |

    gyrwmlclqqf64012791700.png

    gyrwmlclqqf64012791700.png
    5 T+ D$ Q/ C; t2 }
    绘制主、次网格和主、次刻度的极坐标区域。
    & r; i0 R1 z6 G/ [% E' j
  • % 绘制主要的极坐标网格线majorRadius = majorTicksY(majorTicksY > minRadius);for i = 1:length(majorRadius)    [xMajor, yMajor] = pol2cart(deg2rad(allTheta), majorRadius(i));    plot(xMajor, yMajor, '-', 'Color', [0 0 0], 'LineWidth', 0.5);endmajorTheta = windDirections;fullRadius = linspace(minRadius, maxRadius, 1000);for i = 1:length(majorTheta)    [xMajorT, yMajorT] = pol2cart(deg2rad(majorTheta(i)), fullRadius);    plot(xMajorT, yMajorT, '-', 'Color', [0.8 0.8 0.8], 'LineWidth', 1.2);end% 绘制次要的极坐标网格线minorRadius = minorTicksY(minorTicksY > minRadius);for i = 1:length(minorRadius)    [xMinor, yMinor] = pol2cart(deg2rad(allTheta), minorRadius(i));    plot(xMinor, yMinor, ':', 'Color', [0.55 0.55 0.55], 'LineWidth', 0.8);endminorTheta=windDirections+dfT/2;% minorTheta = 11.25:22.5:360;for i = 1:length(minorTheta)    [xMinorT, yMinorT] = pol2cart(deg2rad(minorTheta(i)), fullRadius);    plot(xMinorT, yMinorT, '-.', 'Color', [0.55 0.55 0.55]);end% 绘制最外层的极坐标线[xOuter, yOuter] = pol2cart(deg2rad(allTheta), maxRadius);plot(xOuter, yOuter, '-', 'Color', 'k', 'LineWidth', 2);% 主刻度线mainTickTheta= windDirections+dfT;% mainTickTheta = 0:22.5:360;tickRadius = [maxRadius*0.96, maxRadius];for i = 1:length(mainTickTheta)    [xMainTick, yMainTick] = pol2cart(deg2rad(mainTickTheta(i)), tickRadius);    plot(xMainTick, yMainTick, '-', 'Color', [0, 0, 0], 'LineWidth', 1.5);end% 次刻度线minorTickTheta=windDirections+dfT/2;% minorTickTheta = 11.25:22.5:360;tickRadiusMinor = [maxRadius*0.98, maxRadius];for i = 1:length(minorTickTheta)    [xMinorTick, yMinorTick] = pol2cart(deg2rad(minorTickTheta(i)), tickRadiusMinor);    plot(xMinorTick, yMinorTick, '-', 'Color', [0 0 0], 'LineWidth', 1.5);end% 添加主刻度外的标签labelDist = maxRadius *(1+ 0.094); % 设定标签距离为最大半径(1+ 0.094),可以根据需要进行调整for i = 1:length(majorTheta)    adjustedAngle = majorTheta(i); % 从正北方开始,并且顺时针增加 % 从正北方开始,并且顺时针增加    angle = mod(adjustedAngle, 360); % 确保角度在0-360之间    [xLabel, yLabel] = pol2cart(deg2rad(90-adjustedAngle), labelDist);    labelText = sprintf('%.1f°', angle);    text(xLabel, yLabel, labelText, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');end
    . W2 ^0 v- T' V7 h( R2 I

    dksizia4uwa64012791800.png

    dksizia4uwa64012791800.png

    & J( P4 P% w. A- y: p* z添加刮风数据,添加数据和颜色、图列大小映射关系。
  • adjustedWindDirections = 360 - windDirections;[xWind, yWind] = pol2cart(deg2rad(adjustedWindDirections + 90), windFrequency);colorGradient = [0.0078, 0.0941, 0.7333; 0.9725, 0.0039, 0.5216];colorMapMajor = makeColorMap(colorGradient, length(windDirections));% 根据风速大小对颜色进行排序[~, idx] = sort(maxWindSpeed, 'Ascend');sortedColorMap = colorMapMajor(idx, :);% 对于相同的风速值,使用第一个出现的颜色值为其赋色uniqueSpeeds = unique(maxWindSpeed, 'stable');for i = 1:length(uniqueSpeeds)    currentSpeed = uniqueSpeeds(i);    indices = find(maxWindSpeed == currentSpeed);    if length(indices) > 1        sortedColorMap(indices, :) = repmat(sortedColorMap(indices(1), :), length(indices), 1);    endendmaxWind = max(maxWindSpeed);desiredMaxSize=max(avgWindSpeed);desiredMinSize=min(avgWindSpeed);for i = 1:length(windDirections)    color = getColorForSpeed(maxWindSpeed(i), maxWind , colorMapMajor);    s = mapToMarkerSize(avgWindSpeed(i), desiredMaxSize, desiredMinSize);    plot(xWind(i), yWind(i), 'o', 'LineWidth', 1.2, 'MarkerSize',s, ...        'MarkerEdgeColor', 'k', 'MarkerFaceColor', color);end! U) {9 q' H+ ~# `

    huvlwvr51sw64012791900.png

    huvlwvr51sw64012791900.png

    " i' K5 {0 F- O: }& E% |4 y7 d颜色条绘制
    4 B* ]# w6 ~7 `. {
  • colorMapMinor = makeColorMap(colorGradient, 10);colormap(colorMapMinor);cBar = colorbar;cBar.Position = [.9 .1 .04 .36];cBar.LineWidth = 1.2;cBar.TickLength = 0.025;cBar.Ticks = linspace(floor(min(maxWindSpeed)),ceil(max(maxWindSpeed)),11);% cBar.Ticks =linspace(minWindSpeed, maxWindSpeed, 11);tickLabels = arrayfun(@(x) sprintf('%.1f', x), cBar.Ticks, 'UniformOutput', false);cBar.TickLabels = tickLabels;cBar.TickDirection = 'both';colorBarTitle = title(cBar, '最大风速(m/s)');caxis([floor(min(maxWindSpeed)),ceil(max(maxWindSpeed))]);AxSize=axes(fig,'Position',[.86 0.55 .1 .36],'Color',[1,1,1]);title(AxSize,'平均风速(m/s)')AxSize.XColor='none';AxSize.YColor='none';meanRLabel=linspace(ceil(0.3),ceil(2.6),5);
    : h& |/ v# ]1 Q. p; [! t7 \

    0lpdhgx0rhe64012792000.png

    0lpdhgx0rhe64012792000.png

    / ~" V& u# H+ K2 Q5 n' U8 i添加图列绘制4 b1 q8 \2 i8 G" S* a( M
  • % 定义半径和距离legR=linspace(ceil(min(avgWindSpeed)),ceil(max(avgWindSpeed)),5);d =ceil(ceil(max(avgWindSpeed))/2);% 计算每个圆的中心位置legY = zeros(1, length(legR));totalHeight = sum(2.*legR) + d*(length(legR)-1);legY(1) = totalHeight/2 - legR(1);for i = 2:length(legR)    legY(i) = legY(i-1) - 2*legR(i-1) - d;end% 绘制圆形hold on;for i = 1:length(legR)    rectangle('Position',[-legR(i), legY(i)-legR(i), 2*legR(i), 2*legR(i)], 'Curvature', [1, 1],'LineWidth',1);    text(max(legR) + 0.6, legY(i), sprintf('%.1f', legR(i)));endaxis equal;xlim([-max(legR) max(legR)]);ylim([legY(end)-legR(end) legY(1)+legR(1)]);hold off;/ P1 V9 K: o0 l; W( E: B/ K

    htsn5wgpohm64012792100.png

    htsn5wgpohm64012792100.png
    & D2 z& s: h* U4 R

    3 R' h5 p, e: w+ V/ R. N! Z绘图工具箱介绍默认绘图* y9 t6 q! H. {
  • figure('name','默认绘图')w=WindRosePlot(data);w.plot();9 h0 |" w9 Q4 w9 v4 {' V" W
    / b% A% U! {3 ?5 F9 O! S  {( T9 U/ S
    刻度颜色设置
  • figure('name','设置刻度颜色')w=WindRosePlot(data);w.AxisColor=[0,0,1];w.AxisLineWidth=2;w.plot();$ k5 u& i/ U) J( A- @

    twhr05ju0cp64012792200.png

    twhr05ju0cp64012792200.png
    0 n8 S. M% N* c6 X
    刻度方向和长短设置,其中刻度方向有:'in'|'out'|'both'?。
  • figure('name','设置刻度方向、长短')w=WindRosePlot(data);w.AxisMainTickDir='both';w.AxisMinorTickDir='out';w.AxisColor=[1,0,0];w.AxisLineWidth=2;w.AxisTickLength=0.08;w.plot();
    1 {, y4 u$ c/ \% E

    c2reosjifko64012792301.png

    c2reosjifko64012792301.png
    6 Z4 A8 U! b: i7 {% A# R
    网格设置:主、次网格的颜色、粗细、透明度设置。
  • figure('name','主、次网格样式、颜色、粗细、透明度')w=WindRosePlot(data);w.RGrid='on';w.RGridLineStyle='-.';w.RGridColor=[1,0,0];w.RGridAlpha=0.5;w.RGridLineWidth=1.5;# [$ I. R4 o! g* n$ o4 T
    w.ThetaGrid='on';w.ThetaGridLineStyle='--';w.ThetaGridColor=[0,1,0];w.ThetaGridAlpha=0.5;w.ThetaGridLineWidth=2;
    + x. N, L5 a! [! p! A2 mw.MinorRGrid='on';w.MinorRGridLineStyle='-';w.MinorRGridColor=[1,1,0];. V) G: j9 ?, k  }
    w.MinorThetaGrid='on';w.MinorThetaGridLineStyle=':';w.MinorThetaGridColor=[0,0,1];w.plot();' C- p- X) s  l. @

    hjmph21y2ez64012792401.png

    hjmph21y2ez64012792401.png
    / U* M% t' Z: |
    背景颜色设置
  • figure('name','背景颜色')w=WindRosePlot(data);w.ColorMaps=hsv;w.AxisBackgroundColor=[1,1,1]*0.10;w.AxisFaceAlpha=0.1;w.plot();& s' T  X, @- R+ [( R9 M

    hx5phb3lybt64012792501.png

    hx5phb3lybt64012792501.png
    ' [0 d# e6 o2 B! ?" |3 E" [
    图列设置
  • figure('name','图列设置:title设置、背景色、边缘色、边缘粗细、字体属性')w=WindRosePlot(data);w.LegendTitle='图列设置Title';w.LegendBackgroundColor=[1,.9,.8]*.98;w.LegendEdgeColor='m';w.LegendLineWidth=4;w.LegendFontName='Times new Roman';w.LegendFontSize=16;w.LegendFontAngle='normal';w.LegendFontWeight='bold';%加粗w.plot();/ Q7 g7 D2 P' f9 g7 g

    y4qr340cv2v64012792601.png

    y4qr340cv2v64012792601.png

    2 K3 l! e0 t8 a) a, Q6 F颜色条设置! y# E8 ]) \  q3 X& t  R( t1 [2 }
  • figure('name','颜色条设置')w=WindRosePlot(data);% load('colorsData/acton100.mat')% load('colorsData/bwr.mat')% load('colorsData/vikO100.mat')% w.ColorMaps=colorsList;% w.ColorMaps=hsv;% w.ColorMaps=bone;% w.ColorMaps=jet;% w.ColorMaps=winter;w.ColorMaps=spring;w.ColorMapsTitle='颜色条 Title测试';w.ColorMapsFontSize=14;w.ColorMapsFontAngle='italic';w.plot();
    7 A0 H8 a0 r1 y2 ^# \- c

    n4nmtitiew364012792701.png

    n4nmtitiew364012792701.png

    # V7 S" n8 S5 C% b, t3 M! y- d

    8 @. h7 ]- }0 [$ V

    lsledn3z4qs64012792802.png

    lsledn3z4qs64012792802.png

    9 L* `3 D) M: }- J# f3 y: c( p

    " W+ x4 \7 j" W# l6 p5 K

    hrq5gymper564012792902.png

    hrq5gymper564012792902.png
    : S1 ^$ K. f3 s- H

    ' a5 R. r5 f0 A. t& i/ y3 H5 _

    lncif2swy5w64012793002.png

    lncif2swy5w64012793002.png

    ; A" y7 S# R! {; P6 q+ v0 H
    5 H  t1 {% x8 \  p7 b2 @

    jmtqx1b0tgj64012793102.png

    jmtqx1b0tgj64012793102.png
    - ?& `- E6 J* L2 Q
    - ~1 b$ ]6 j/ ^  B
    $ s( ~7 E$ _6 v! s5 t) O6 |
    WindRosePlot类函数收藏=学会2 T' S8 d0 C1 x1 x; {- L: N$ Z
  • classdef WindRosePlot    % ------------------------------------------------    %  @Author:  好玩的 MATLAB.    %  @E-mail:  2377389590@qq.com    %  @WeChat:  idmatlab    %  @Date:    Oct 30, 2023.    %  @Version: Matlab R2022b.    %  % #Respect the fruits of labor. Please note the tweet link and official account name when reprinting. Commercial use is strictly prohibited.    %  Example:               %     windDirections = [0.0, 22.5, 45.0, 67.5, 90.0, 112.5, 135.0, 157.5, 180.0, 202.5, 225.0, 247.5, 270.0, 292.5, 315.0, 337.5]';    %     windFrequency = [10.2, 4, 4, 3.0, 4.0, 3.0, 4.0, 4.0, 7.0, 6.0, 3, 2, 2, 1.0, 3, 5]';    %     maxWindSpeed = [15, 12, 10, 5, 5, 5,10, 12, 13, 15, 12,5, 5, 10, 12,  15]';    %     avgWindSpeed = [2.8, 2.0, 1.5, 1.2, 1.4, 1.3, 1.45, 2.00, 2.80, 2.80, 1.50, 1.00, 1.0, 1.0, 1.8, 2.0]';    %     % dataT = table(windDirections, windFrequency, maxWindSpeed, avgWindSpeed, 'VariableNames', {'风向', '频率', '最大风速', '平均风速'});    %     data=[windDirections,windFrequency,maxWindSpeed,avgWindSpeed];    %     w=WindRosePlot(data);    %     w.plot();    %-------------------------------------------------    properties        fig        %        WindDirections;     %风向        WindFrequency;      %刮风频率        MaxWindSpeed;       %最大风速        AvgWindSpeed;       %平均风速        %        RGrid='on';         %显示主 r 轴网格线        ThetaGrid='on';     %显示主 Theta 轴网格线        MinorRGrid='on';    %显示次 r 轴网格线        MinorThetaGrid='on';%显示次 Theta 轴网格线        %        YLabel='风向频率(%)';        %  R主网格样式        RGridLineStyle='-';        RGridColor=[0,0,0];        RGridAlpha=1;        RGridLineWidth=0.5;        % Theta 主网格样式        ThetaGridLineStyle='-';        ThetaGridColor=[0.8 0.8 0.8];        ThetaGridAlpha=1;        ThetaGridLineWidth=1.2;        % R次网格样式        MinorRGridLineStyle=':';        MinorRGridColor=[0.55 0.55 0.55];        MinorRGridAlpha=1;        MinorRGridLineWidth=0.8;        % Theta 次网格样式        MinorThetaGridLineStyle='-.'        MinorThetaGridColor=[0.55 0.55 0.55];        MinorThetaGridAlpha=1;        MinorThetaGridLineWidth=1.2;        % 刻度样式        AxisColor=[0,0,0];        AxisLineWidth=1.5;        AxisMainTickDir='in';        AxisMinorTickDir='in';        AxisTickLength=0.04;        AxisTickFontSize=12;        AxisTickFontWeight='normal'%'normal'|'bold'        AxisTickFontName='Times new Roman';        AxisFaceAlpha=0.5;        AxisBackgroundColor=[1,1,1];        % 颜色条        ColorMaps=[0.0078, 0.0941, 0.7333; 0.9725, 0.0039, 0.5216];        ColorMapsTitle='最大风速(m/s)';        ColorMapsFontName='Times new Roman';        ColorMapsFontSize=12;        ColorMapsFontAngle='normal';%'normal' | 'italic'。        ColorMapsFontWeight='normal'; %'normal'|'bold'        %legend 图列        LegendTitle='平均风速(m/s)';        LegendBackgroundColor=[1,1,1]*.95;        LegendEdgeColor='none';        LegendLineWidth=1;        LegendFontName='Times new Roman';        LegendFontAngle='italic';%'normal' | 'italic'。        LegendFontSize=12;        LegendFontWeight='normal';        LegendRotation=0;    end    methods        function  obj =WindRosePlot(data)            obj.fig=gcf;            obj.fig.Color=[1,1,1];            obj.WindDirections = data(:,1);            obj.WindFrequency = data(:,2);            obj.MaxWindSpeed = data(:,3);            obj.AvgWindSpeed =data(:,4);            %-------------------------            obj.RGrid='on';         obj.RGridLineStyle='-';          obj.RGridColor=[0,0,0];                  obj.RGridAlpha=1;         obj.RGridLineWidth=0.5;            obj.ThetaGrid='on';     obj.ThetaGridLineStyle='-';      obj.ThetaGridColor=[0.8 0.8 0.8];        obj.ThetaGridAlpha=1;     obj.ThetaGridLineWidth=1.2;            obj.MinorRGrid='on';    obj.MinorRGridLineStyle=':';     obj.MinorRGridColor=[0.55 0.55 0.55];    obj.MinorRGridAlpha=1;    obj.MinorRGridLineWidth=0.8;            obj.MinorThetaGrid='on';obj.MinorThetaGridLineStyle='-.';obj.MinorThetaGridColor=[0.55 0.55 0.55];obj.MinorThetaGridAlpha=1;obj.MinorThetaGridLineWidth=1.2;            obj.ColorMaps = [0.0078, 0.0941, 0.7333; 0.9725, 0.0039, 0.5216];        end        function plot(obj)            maxRadius = ceil(max(obj.WindFrequency));            minRadius = round(maxRadius / 10);            dfT=unique(diff(obj.WindDirections));            allTheta = 0:0.01:360;            ax = gca;            hold on; box off; grid off; axis equal;            ax.YLim = [-maxRadius, maxRadius];            ax.YMinorTick = 'on';            ax.XColor = 'none';            ax.XDir = 'normal';            ax.TickDir = 'out';            ax.LineWidth = obj.AxisLineWidth;            ax.YColor=obj.AxisColor;            majorTicksY = ax.YTick;            minorTicksY = (majorTicksY(1:end-1) + majorTicksY(2:end)) / 2;            diffTicks = unique(diff(majorTicksY));            maxRadius=max(ax.YTick)+diffTicks/2;            ax.YLim = [-maxRadius, maxRadius];%重新设定            ax.YAxis.MinorTickValues = [minorTicksY(1)-diffTicks, minorTicksY, minorTicksY(end)+diffTicks];            ax.YTickLabel = arrayfun(@(y) sprintf('%d', abs(y)), majorTicksY, 'UniformOutput', false);            ax.YLabel.String=obj.YLabel;            % =============绘制主要R Theta的极坐标网格线===============================            %  # RGrid            majorRadius = majorTicksY(majorTicksY > minRadius);            if strcmpi(obj.RGrid,'on')                for i = 1:length(majorRadius)                    [xMajor, yMajor] = pol2cart(deg2rad(allTheta), majorRadius(i));                    plot(xMajor, yMajor,'LineStyle',obj.RGridLineStyle,'Color', [obj.RGridColor obj.RGridAlpha], 'LineWidth',obj.RGridLineWidth);                end            elseif strcmpi(obj.RGrid,'off')            end            %   # ThetaGrid            majorTheta = obj.WindDirections;            fullRadius = linspace(minRadius, maxRadius, 1000);            if strcmpi(obj.ThetaGrid,'on')                for i = 1:length(majorTheta)                    [xMajorT, yMajorT] = pol2cart(deg2rad(majorTheta(i)), fullRadius);                    plot(xMajorT, yMajorT, 'LineStyle',obj.ThetaGridLineStyle, 'Color', [obj.ThetaGridColor obj.ThetaGridAlpha], 'LineWidth', obj.ThetaGridLineWidth);                end            elseif  strcmpi(obj.ThetaGrid,'off')            end            % 绘制次要R Theta的极坐标网格线            %   # MinorRGrid            if strcmpi(obj.MinorRGrid,'on')                minorRadius = minorTicksY(minorTicksY > minRadius);                for i = 1:length(minorRadius)                    [xMinor, yMinor] = pol2cart(deg2rad(allTheta), minorRadius(i));                    plot(xMinor, yMinor, 'LineStyle',obj.MinorRGridLineStyle,'Color', [obj.MinorRGridColor obj.MinorRGridAlpha], 'LineWidth',obj.MinorRGridLineWidth);                end            elseif strcmpi(obj.MinorRGrid,'off')            end            %   # MinorThetaGrid            minorTheta=obj.WindDirections+dfT/2;            if strcmpi(obj.MinorThetaGrid,'on')                for i = 1:length(minorTheta)                    [xMinorT, yMinorT] = pol2cart(deg2rad(minorTheta(i)), fullRadius);                    plot(xMinorT, yMinorT, 'LineStyle',obj.MinorThetaGridLineStyle, 'Color', [obj.MinorThetaGridColor obj.MinorThetaGridAlpha], 'LineWidth', obj.MinorThetaGridLineWidth);                end            elseif strcmpi(obj.MinorThetaGrid,'off')            end            % ===================绘制刻度===============================            % 绘制最外层的极坐标线            [xOuter, yOuter] = pol2cart(deg2rad(allTheta), max(ax.YTick)+diffTicks/2);            plot(xOuter, yOuter, '-', 'Color',obj.AxisColor, 'LineWidth', obj.AxisLineWidth);            fill(xOuter, yOuter,obj.AxisBackgroundColor,'EdgeColor','none','FaceAlpha',obj.AxisFaceAlpha)            % #主刻度            if obj.AxisTickLength > 1 || obj.AxisTickLength 0                error('TickLength must be less than 1 and greater than 0.');            end            if strcmpi(obj.AxisMainTickDir,'in')                tickRadius = [maxRadius*(1-obj.AxisTickLength), maxRadius];            elseif strcmpi(obj.AxisMainTickDir,'out')                tickRadius = [ maxRadius,maxRadius*(1+obj.AxisTickLength)];            elseif strcmpi(obj.AxisMainTickDir,'both')                tickRadius = [maxRadius*(1-obj.AxisTickLength) maxRadius*(1+obj.AxisTickLength)];            else                error('Invalid value for mainTickDir. It should be "in", "out", or "both".');            end            mainTickTheta= obj.WindDirections+dfT;            for i = 1:length(mainTickTheta)                [xMainTick, yMainTick] = pol2cart(deg2rad(mainTickTheta(i)), tickRadius);                plot(xMainTick, yMainTick, '-', 'Color', obj.AxisColor, 'LineWidth', obj.AxisLineWidth);            end            disp(char([20844 20247 21495 58 22909 29609 30340 77 97 116 108 97 98]))            % #次刻度            if strcmpi(obj.AxisMinorTickDir,'in')                tickRadiusMinor = [maxRadius*(1-obj.AxisTickLength/2), maxRadius];            elseif strcmpi(obj.AxisMinorTickDir,'out')                tickRadiusMinor = [ maxRadius maxRadius*(1+obj.AxisTickLength/2)];            elseif strcmpi(obj.AxisMinorTickDir,'both')                tickRadiusMinor = [maxRadius*(1-obj.AxisTickLength/2), maxRadius*(1+obj.AxisTickLength/2)];            else                error('Invalid value for mainTickDir. It should be "in", "out", or "both".');            end            minorTickTheta=obj.WindDirections+dfT/2;            for i = 1:length(minorTickTheta)                [xMinorTick, yMinorTick] = pol2cart(deg2rad(minorTickTheta(i)), tickRadiusMinor);                plot(xMinorTick, yMinorTick, '-', 'Color', obj.AxisColor, 'LineWidth', obj.AxisLineWidth*0.8);            end            % ==========添加主刻度外的标签===========================            labelDist = maxRadius *(1+ 0.094); % 设定标签距离为最大半径(1+ 0.094),可以根据需要进行调整            for i = 1:length(majorTheta)                adjustedAngle = majorTheta(i); % 从正北方开始,并且顺时针增加 % 从正北方开始,并且顺时针增加                angle = mod(adjustedAngle, 360); % 确保角度在0-360之间                [xLabel, yLabel] = pol2cart(deg2rad(90-adjustedAngle), labelDist);                labelText = sprintf('%.1f°', angle);                text(xLabel, yLabel, labelText, 'HorizontalAlignment', 'center', ...                    'VerticalAlignment', 'middle', ...                    'FontSize',obj.AxisTickFontSize,'FontWeight',obj.AxisTickFontWeight,'FontName',obj.AxisTickFontName, ...                    'Color',obj.AxisColor);            end            %==================添加数据========================            adjustedWindDirections = 360 - obj.WindDirections;            [xWind, yWind] = pol2cart(deg2rad(adjustedWindDirections + 90), obj.WindFrequency);            colorMapMajor = makeColorMap(obj.ColorMaps, length(obj.WindDirections));            % 根据风速大小对颜色进行排序            [~, idx] = sort(obj.MaxWindSpeed, 'Ascend');            sortedColorMap = colorMapMajor(idx, :);            %对于相同的风速值,使用第一个出现的颜色值为其赋色            uniqueSpeeds = unique(obj.MaxWindSpeed, 'stable');            for i = 1:length(uniqueSpeeds)                currentSpeed = uniqueSpeeds(i);                indices = find(obj.MaxWindSpeed == currentSpeed);                if length(indices) > 1                    sortedColorMap(indices, :) = repmat(sortedColorMap(indices(1), :), length(indices), 1);                end            end            maxWind = max(obj.MaxWindSpeed);            desiredMaxSize=max(obj.AvgWindSpeed);            desiredMinSize=min(obj.AvgWindSpeed);            for i = 1:length(obj.WindDirections)                color = obj.getColorForSpeed(obj.MaxWindSpeed(i), maxWind , colorMapMajor);                s = obj.mapToMarkerSize(obj.AvgWindSpeed(i), desiredMaxSize, desiredMinSize);                plot(xWind(i), yWind(i), 'o', 'LineWidth', 1.2, 'MarkerSize',s,...                    'MarkerEdgeColor', 'k', 'MarkerFaceColor', color);            end            % ================添加 colorbar======================================            colorMapMinor = makeColorMap(obj.ColorMaps, 10);            colormap(colorMapMinor);            cBar = colorbar;            cBar.Position = [.9 .1 .04 .36];            cBar.LineWidth = 1.2;            cBar.TickLength = 0.025;            cBar.Ticks = linspace(floor(min(obj.MaxWindSpeed)),ceil(max(obj.MaxWindSpeed)),11);            tickLabels = arrayfun(@(x) sprintf('%.1f', x), cBar.Ticks, 'UniformOutput', false);            cBar.TickLabels = tickLabels;            cBar.TickDirection = 'both';            caxis([floor(min(obj.MaxWindSpeed)),ceil(max(obj.MaxWindSpeed))]);            title(cBar, obj.ColorMapsTitle);            cBar.FontName= obj.ColorMapsFontName;            cBar.FontSize=obj.ColorMapsFontSize;            cBar.FontAngle= obj.ColorMapsFontAngle;            cBar.FontWeight=obj.ColorMapsFontWeight; %'normal'|'bold'            % ===============添加 Legend=======================================            legAx=axes(obj.fig,'Position',[.86 0.55 .1 .36],'Color',[1,1,1],'Box','on');            title(legAx,obj.LegendTitle)            legAx.Color=obj.LegendBackgroundColor;            legAx.XColor=obj.LegendEdgeColor;legAx.YColor=obj.LegendEdgeColor;            legAx.XTick=[];legAx.YTick=[];            legAx.TickLength=[0 0];            legAx.LineWidth=obj.LegendLineWidth;                 % 定义半径和距离            legR=linspace(ceil(min(obj.AvgWindSpeed)),ceil(max(obj.AvgWindSpeed)),5);            d =ceil(ceil(max(obj.AvgWindSpeed))/2);            legY = zeros(1, length(legR));            totalHeight = sum(2.*legR) + d*(length(legR)-1);            legY(1) = totalHeight/2 - legR(1);            for i = 2:length(legR)                legY(i) = legY(i-1) - 2*legR(i-1) - d;            end            % 绘制圆形            hold on;            for i = 1:length(legR)                rectangle('Position',[-legR(i), legY(i)-legR(i), 2*legR(i), 2*legR(i)], 'Curvature', [1, 1],'LineWidth',1.5,'FaceColor', 'white');                text(max(legR) + 0.9, legY(i), sprintf('%.1f', legR(i)), ...                    "FontName",obj.LegendFontName,"FontSize",obj. LegendFontSize,'FontAngle',obj.LegendFontAngle, ...                    'FontWeight',obj.LegendFontWeight,'Rotation',obj.LegendRotation);            end            axis equal;            xlim([-max(legR)*1.2 max(legR)*1.2]);            ylim([(legY(end)-legR(end))*1.1 (legY(1)+legR(1))*1.1]);            hold off;        end    end    methods(Access=private)        function color = getColorForSpeed(obj,speed, maxSpeed, colorMap)            idx = round((speed / maxSpeed) * size(colorMap, 1));            idx = min(max(idx, 1), size(colorMap, 1)); % 保证idx在正确的范围内            color = colorMap(idx, :);        end        function s = mapToMarkerSize(obj,speed, maxSize, minSize)            normalized = (speed - minSize)/(maxSize -minSize);            s = minSize + normalized * (maxSize - minSize);            s=s*10;        end    endend属性:fig:存储图形的句柄。7 f2 ?) y: j  ]: N* M. k+ v! N
    WindDirections:风向数据。
    1 w9 p/ W9 C: \4 }. RWindFrequency:刮风频率。
    $ d' E/ ?! X* k$ P% RMaxWindSpeed:最大风速数据。0 v/ z( Q) m5 C9 J
    AvgWindSpeed:平均风速数据。
    * P3 W, O3 L5 T- K3 CRGrid,ThetaGrid,MinorRGrid,MinorThetaGrid:这些属性用于控制是否显示对应的网格线。
    " X* t$ T' t4 B' F" M# v. [YLabel:Y轴标签。
    2 `7 f( Z+ ?* y5 M还有许多其他的属性,例如RGridLineStyle,RGridColor等,用于控制图形的样式和外观。2 _7 M- o4 w- \! Q2 h4 q
    方法:构造函数WindRosePlot:当创建WindRosePlot类的对象时,此构造函数将被调用。构造函数接收风玫瑰数据作为输入,并初始化类的属性。此外,还设置了默认的图形样式。! G( k. l  f* v  h# y
    plot方法:这个方法用于绘制风玫瑰图。它首先设置坐标轴和极坐标的属性。接下来,它会绘制主要和次要的R和Theta网格线。然后,它绘制刻度。
    6 D, X, e0 L$ h3 o; C6 R5 ]" n( N" Q, Z; J7 k5 w
    [/ol]- -THE END- -
    * E- m. f: \9 Y, h0 U3 \源码下载:gitee下载:https://gitee.com/iDMatlab/wind-rose-plot
    0 k! a! x4 R* M% {5 [0 E
    & }2 D; c% i5 x0 ?& [7 j# XQQ 群下载4 l# y- R) z5 z7 I9 A& y
    4 j7 a, `& J% O9 Y
    % G+ X8 x4 @" [- @' ?, D( P

    . b* G+ p2 i* L0 p& N: J. y
    " e# f4 S7 R* U
    , E& l, I% H( X4 V3 c$ I; g7 W送书活动6 l3 O- O. E9 ?9 H

    ) m' L4 c* h6 I9 e2 }3 k, ?3 b
    6 K9 f0 g( f, d+ u) B
    3 h! I' ?0 s) ^, V3 a( Y包邮赠送 「北京大学出版社赞助《AI时代程序员开发之道》和《AI时代项目经理成长之道》本书《AI时代程序员开发之道》:知识精进+重点解析+上机实训+综合实战+ChatGPT应用,零基础入门,让程序员插上翅膀高效开发!本书《AI时代项目经理成长之道》:1.开发新模式:让项目管理更高效、更快捷、更完美。 2.全流程解析:涵盖ChatGPT的不同应用场景,从编写各种文档,到组建高效团队、辅助项目沟通管理、计划管理、时间管理、质量管理等使用ChatGPT进行程序开发的关键技巧。 3.实战检验:ChatGPT结合多种项目管理工具及案例实操讲解,理解更加透彻。 4.100%提高开发效率:揭秘ChatGPT与项目管理工作高效融合的核心方法论和实践经验。 5.超值资源:免费赠送全书案例源文件、教学视频及配套工具,供读者下载学习。$ A/ x- V1 X. J1 W+ O& L7 O

    9 K2 [( B6 y* W+ O7 @6 C' i0 |, b- `' E; r' q
    【抽奖方式及满足条件】:
    1 [! s0 p# ]& |* p) \- ?" t, N1.关注「好玩的MATLAB 」公众号和视频号* P( |9 @) e6 i8 i
    2 Z! C* o# S* n8 ?4 r0 T4 T" u
    2.给本文点【】+【在看】;  }4 i4 g( @5 V3 Q9 _! o0 b
    3.留言区评论点赞最多的前3名。- d  x7 m% |9 b1 o6 r8 n0 h
    4.本活动只针对从未获过奖的同学,之前获过奖的小伙伴,不用参加。0 i3 \& K9 a5 W$ m% G; J
    同时满足上述4个条件的读者朋友,包邮赠送一本书。. @3 g5 H/ ]5 J7 L1 r
    【开奖时间】:2023年10月31日夜晚8点
    ' j' m2 N8 e' E2 |/ [8 X: r6 a【领奖方式】:在开奖时加小编私人微信:idmatlab
    9 d  r) N5 g" s, f1 S6 X9 {1 H; _1 Z9 L, b* j3 F
    扫一扫加管理员微信. R& K& X  ?5 m' h, K1 j

    2 |0 P9 p! }/ o0 g
  • 回复

    使用道具 举报

    发表回复

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则


    联系客服 关注微信 下载APP 返回顶部 返回列表