|
点击上方蓝字和“好玩的MATLAB”一起快乐玩耍吧!% n! j. p! d/ y4 t
qlctn554ait64017651958.jpg
2 Y2 j( Z+ ?/ t. f; o/ {% c+ O
好玩的matlab
$ |. x4 a4 W% T6 i6 H带你解锁不一样的matlab新玩法
$ O) w9 k$ c1 O7 Q/ ?0 j
3 @+ }' f/ g- Q% P3 _# n半个月前有粉丝问我怎么画”风玫瑰图“,之前因为时间比较忙所以没有更新,今天有时间抽空介绍一下“风玫瑰图”的画法,喜欢此推文的小伙伴们记得点赞+关注+分享!【尊重作者劳动成果,转载请注明推文链接和公众号名】
9 b& V& J2 Y/ M* Q& `9 m/ r/ N粉丝给的图:
4 i# o3 m4 B/ [1 R* ?$ H( c
ehhx0itnplp64017652058.png
+ W3 R0 N% @0 H% G( f; i7 e, Y
复刻的图:& T6 h/ c% T9 x7 Y& m+ H$ [/ Z- s
u0wt5hs3dxz64017652158.png
2 j* \* y+ n" y5 \6 u其他样式效果:$ G- V% R0 R8 ]: M7 {& U2 d5 b
gmsrseu5co464017652258.gif
. W/ k' K( G5 ]! L( a
6 X$ w, y8 [( k) Q4 A
3 `& m4 f4 F9 ]数据数据包含:风向、刮风频率、最大风速、平均风速四个数据。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];数据长成这样的: i% ^; h5 X6 }0 W, v7 }
tjxlg35tbo164017652358.png
' ^, G; B& H2 J; b; g. z" l
9 u( j4 W8 N4 z8 P9 u; l
绘图教程绘制左边Y轴
: O& c2 |/ u v% L! a4 G N% b% 设置基础参数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='风向频率(%)';; A6 n# G- S* R% s) ?9 x
da2jodx2msu64017652458.png
5 S4 X: s* k" u绘制主、次网格和主、次刻度的极坐标区域。
; @; f8 U* `) x* K) t% 绘制主要的极坐标网格线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
3 J9 ~8 N, _0 l! i( C" d% B
t5y0iuqaslw64017652558.png
: U9 x" Y- j! p$ D添加刮风数据,添加数据和颜色、图列大小映射关系。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+ ?' N& l4 M! p3 I ^! u; l
523ofdy5azc64017652658.png
Z0 S3 Z7 S; M) z* ^6 p, N# _颜色条绘制
* s- G7 C- _5 r2 ?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);
O6 c5 G$ q5 [4 V) }3 {
vjxuvwnbbms64017652758.png
/ ?: | `) y4 L, {3 M+ P' o2 L# D
添加图列绘制& Z! W' Z* h3 k- m# K8 O
% 定义半径和距离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;! t# @/ M8 h6 X0 a) Y* y( }# m8 j
hq40hx23tan64017652859.png
* I$ ~- C& E( K0 W0 w1 y0 N- K5 a; G. `
绘图工具箱介绍默认绘图( f9 S5 ?+ o: w7 W
figure('name','默认绘图')w=WindRosePlot(data);w.plot();
) W% @! \8 U( R( s$ |6 F7 _
3 B; r" z+ f. Q, U$ d( O, _刻度颜色设置figure('name','设置刻度颜色')w=WindRosePlot(data);w.AxisColor=[0,0,1];w.AxisLineWidth=2;w.plot();
! M- U2 P# K+ I: `; g
jkoetja3qib64017652959.png
1 ^! e8 P, D F4 T7 ~0 N刻度方向和长短设置,其中刻度方向有:'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();5 Y- X; f2 i" ?$ u9 ^; b
jic0r3zgow464017653059.png
1 V) P3 c, T2 p
网格设置:主、次网格的颜色、粗细、透明度设置。figure('name','主、次网格样式、颜色、粗细、透明度')w=WindRosePlot(data);w.RGrid='on';w.RGridLineStyle='-.';w.RGridColor=[1,0,0];w.RGridAlpha=0.5;w.RGridLineWidth=1.5;8 M# G2 t" h* T! M1 G4 _
w.ThetaGrid='on';w.ThetaGridLineStyle='--';w.ThetaGridColor=[0,1,0];w.ThetaGridAlpha=0.5;w.ThetaGridLineWidth=2;
8 P* M1 E; G7 ]( ?% mw.MinorRGrid='on';w.MinorRGridLineStyle='-';w.MinorRGridColor=[1,1,0];
$ C3 V' a7 O9 Q' Y8 V4 Kw.MinorThetaGrid='on';w.MinorThetaGridLineStyle=':';w.MinorThetaGridColor=[0,0,1];w.plot();
' a" d7 l) e" Z% G
sru15f1cwcx64017653159.png
1 ]7 ]8 ?& ?3 A5 q# V Q, a2 b$ d1 A
背景颜色设置figure('name','背景颜色')w=WindRosePlot(data);w.ColorMaps=hsv;w.AxisBackgroundColor=[1,1,1]*0.10;w.AxisFaceAlpha=0.1;w.plot();
0 f4 B0 D" p& C! E+ i* M
1wo3fm4ct4c64017653259.png
+ B7 O( j& z I# B( H图列设置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();! V9 ?. P4 p# K' c6 d: I1 j, [* K
1053ucxbx3h64017653359.png
$ @, f2 y+ X6 k& }" |颜色条设置
" o9 X7 ~7 p, b: v+ ~/ ^5 I1 ^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();" u) D, B- |, {6 y; |/ l+ v9 M8 U! v
mxykuzq4ss364017653459.png
, S" t2 b- `3 s4 r
|
7 b4 r% _- c) [. D- R
xkhlamwi1by64017653559.png
) _$ W, i' I* P% d
| & P6 K! w9 q1 D8 O' p% V8 c2 o3 x
qqs2epidb1264017653600.png
2 |/ Z: w& \/ b3 C/ i8 \6 j |
* X7 l- w0 o9 x7 L$ n
0rnhzg1flv064017653700.png
) b2 ^, E7 m. Q5 T' h% c/ g |
1 k M! Z+ g* f4 \
4dijdknvmcf64017653800.png
5 u( x" N6 ?$ _6 L4 W3 X
| & f5 j# u, N1 H/ Y, Y
* t/ l7 [! U2 u, F6 A
| WindRosePlot类函数收藏=学会
6 a( k3 |/ [/ w7 g3 mclassdef 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:存储图形的句柄。
. j+ A! n! q; g- kWindDirections:风向数据。
$ g" p0 j N( IWindFrequency:刮风频率。 ^" n. c2 I& R8 g
MaxWindSpeed:最大风速数据。
- t9 i4 C }2 j# vAvgWindSpeed:平均风速数据。
4 ~) I l3 i! [" C5 I- k1 RRGrid,ThetaGrid,MinorRGrid,MinorThetaGrid:这些属性用于控制是否显示对应的网格线。4 A# x5 @" ^0 Z2 o# I4 x
YLabel:Y轴标签。
' }8 K& ~* N! |0 O4 b( O/ T+ h还有许多其他的属性,例如RGridLineStyle,RGridColor等,用于控制图形的样式和外观。
( g# m: X; p# ^/ X, u% h7 B方法:构造函数WindRosePlot:当创建WindRosePlot类的对象时,此构造函数将被调用。构造函数接收风玫瑰数据作为输入,并初始化类的属性。此外,还设置了默认的图形样式。, w5 @1 T' }9 N5 Q4 Z7 C
plot方法:这个方法用于绘制风玫瑰图。它首先设置坐标轴和极坐标的属性。接下来,它会绘制主要和次要的R和Theta网格线。然后,它绘制刻度。
7 V, T: C: n$ Q; s' V, D: i+ q5 h( N; {( ]3 p$ q5 m, ^# x6 E: b' B
[/ol]- -THE END- -/ m& j0 N9 T' z; ~, J$ H/ q
源码下载:gitee下载:https://gitee.com/iDMatlab/wind-rose-plot" N1 h, j w5 z8 Y$ S0 v

( @9 Z0 `7 m& JQQ 群下载
% k' Z1 [2 B! S 7 o0 l! u# r' }
# T) y& i0 U4 L# l( O+ o" i: X) w' s- I- W: ~. x7 ^
3 N$ o# W2 J; m- T. e: o4 |8 F

: o. I: t/ X+ _: l, l! v送书活动
: h: t( Q& D% F$ v2 m$ v8 h
+ P3 {' N: t( ?- F a& L+ r6 m
* p9 A+ p6 d( ]9 }; P! r( ~' f3 r2 e$ B; g$ H+ P9 o5 F
包邮赠送 「北京大学出版社」赞助《AI时代程序员开发之道》和《AI时代项目经理成长之道》本书《AI时代程序员开发之道》:知识精进+重点解析+上机实训+综合实战+ChatGPT应用,零基础入门,让程序员插上翅膀高效开发!本书《AI时代项目经理成长之道》:1.开发新模式:让项目管理更高效、更快捷、更完美。 2.全流程解析:涵盖ChatGPT的不同应用场景,从编写各种文档,到组建高效团队、辅助项目沟通管理、计划管理、时间管理、质量管理等使用ChatGPT进行程序开发的关键技巧。 3.实战检验:ChatGPT结合多种项目管理工具及案例实操讲解,理解更加透彻。 4.100%提高开发效率:揭秘ChatGPT与项目管理工作高效融合的核心方法论和实践经验。 5.超值资源:免费赠送全书案例源文件、教学视频及配套工具,供读者下载学习。" v' v5 m! v4 R5 ?. W' @2 h4 o
1 q' \) p5 M/ C! V+ v( y/ A) [0 D" F8 j; l# Z
【抽奖方式及满足条件】:: V( [$ U; x7 g- Y( N
1.关注「好玩的MATLAB 」公众号和视频号
+ Z5 ]5 e! ~ V) d% y
. X5 Z! Y5 b R: b) A2.给本文点【赞】+【在看】; ]+ y) E+ ]( g" b# h1 b: v
3.留言区评论点赞最多的前3名。
0 q& y" E% |: t+ L+ D4.本活动只针对从未获过奖的同学,之前获过奖的小伙伴,不用参加。9 ] d/ c! ]/ j
同时满足上述4个条件的读者朋友,包邮赠送一本书。% z% y0 J: @$ h# B' X1 L- E
【开奖时间】:2023年10月31日夜晚8点" V! }- G+ J- z/ B
【领奖方式】:在开奖时加小编私人微信:idmatlab8 R1 q% w6 w# M' f% x8 A2 _
: o, b* Q9 O% n- H0 x- B扫一扫加管理员微信
0 ~4 A6 Z+ S; y! H9 E6 v 5 j( y! i0 o: o+ T' c8 Z
 |
|