點擊上方藍(lán)字和“好玩的MATLAB”一起快樂玩耍吧!
zqxcgscnlpw6403000656.jpg (236.67 KB, 下載次數(shù): 1)
下載附件
保存到相冊
zqxcgscnlpw6403000656.jpg
2024-9-3 17:21 上傳
好玩的matlab
帶你解鎖不一樣的matlab新玩法
半個月前有粉絲問我怎么畫”風(fēng)玫瑰圖“,之前因為時間比較忙所以沒有更新,今天有時間抽空介紹一下“風(fēng)玫瑰圖”的畫法,喜歡此推文的小伙伴們記得點贊+關(guān)注+分享!【尊重作者勞動成果,轉(zhuǎn)載請注明推文鏈接和公眾號名】
粉絲給的圖:
34nfdwbctio6403000757.png (284.41 KB, 下載次數(shù): 0)
下載附件
保存到相冊
34nfdwbctio6403000757.png
2024-9-3 17:21 上傳
復(fù)刻的圖:
srhsgzhrb1n6403000857.png (94.93 KB, 下載次數(shù): 0)
下載附件
保存到相冊
srhsgzhrb1n6403000857.png
2024-9-3 17:21 上傳
其他樣式效果:
cgk2mcjyrsn6403000957.gif (1.3 MB, 下載次數(shù): 3)
下載附件
保存到相冊
cgk2mcjyrsn6403000957.gif
2024-9-3 17:21 上傳
數(shù)據(jù)數(shù)據(jù)包含:風(fēng)向、刮風(fēng)頻率、最大風(fēng)速、平均風(fēng)速四個數(shù)據(jù)。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];數(shù)據(jù)長成這樣的
r5hbiadru1t6403001057.png (17.73 KB, 下載次數(shù): 3)
下載附件
保存到相冊
r5hbiadru1t6403001057.png
2024-9-3 17:21 上傳
繪圖教程繪制左邊Y軸
% 設(shè)置基礎(chǔ)參數(shù)maxRadius = ceil(max(windFrequency));minRadius = round(maxRadius / 10);dfT=unique(diff(windDirections));allTheta = 0:0.01:360;% 創(chuàng)建并配置坐標(biāo)軸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];%重新設(shè)定ax.YAxis.MinorTickValues = [minorTicksY(1)-diffTicks, minorTicksY, minorTicksY(end)+diffTicks];ax.YTickLabel = arrayfun(@(y) sprintf('%d', abs(y)), majorTicksY, 'UniformOutput', false);ax.YLabel.String='風(fēng)向頻率(%)';
tqjnqiyynbm6403001158.png (9.23 KB, 下載次數(shù): 2)
下載附件
保存到相冊
tqjnqiyynbm6403001158.png
2024-9-3 17:21 上傳
繪制主、次網(wǎng)格和主、次刻度的極坐標(biāo)區(qū)域。
% 繪制主要的極坐標(biāo)網(wǎng)格線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% 繪制次要的極坐標(biāo)網(wǎng)格線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% 繪制最外層的極坐標(biāo)線[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% 添加主刻度外的標(biāo)簽labelDist = maxRadius *(1+ 0.094); % 設(shè)定標(biāo)簽距離為最大半徑(1+ 0.094),可以根據(jù)需要進(jìn)行調(diào)整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
aglx5x1cubp6403001258.png (87.5 KB, 下載次數(shù): 1)
下載附件
保存到相冊
aglx5x1cubp6403001258.png
2024-9-3 17:21 上傳
添加刮風(fēng)數(shù)據(jù),添加數(shù)據(jù)和顏色、圖列大小映射關(guān)系。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));% 根據(jù)風(fēng)速大小對顏色進(jìn)行排序[~, idx] = sort(maxWindSpeed, 'Ascend');sortedColorMap = colorMapMajor(idx, :);% 對于相同的風(fēng)速值,使用第一個出現(xiàn)的顏色值為其賦色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
efasx5b3vn16403001358.png (97.48 KB, 下載次數(shù): 2)
下載附件
保存到相冊
efasx5b3vn16403001358.png
2024-9-3 17:21 上傳
顏色條繪制
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, '最大風(fēng)速(m/s)');caxis([floor(min(maxWindSpeed)),ceil(max(maxWindSpeed))]);AxSize=axes(fig,'Position',[.86 0.55 .1 .36],'Color',[1,1,1]);title(AxSize,'平均風(fēng)速(m/s)')AxSize.XColor='none';AxSize.YColor='none';meanRLabel=linspace(ceil(0.3),ceil(2.6),5);
hmukgrvcykx6403001458.png (105.07 KB, 下載次數(shù): 1)
下載附件
保存到相冊
hmukgrvcykx6403001458.png
2024-9-3 17:21 上傳
添加圖列繪制
% 定義半徑和距離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;
3o2vxwhxfnp6403001558.png (109.64 KB, 下載次數(shù): 0)
下載附件
保存到相冊
3o2vxwhxfnp6403001558.png
2024-9-3 17:21 上傳
繪圖工具箱介紹默認(rèn)繪圖
figure('name','默認(rèn)繪圖')w=WindRosePlot(data);w.plot();
刻度顏色設(shè)置figure('name','設(shè)置刻度顏色')w=WindRosePlot(data);w.AxisColor=[0,0,1];w.AxisLineWidth=2;w.plot();
h5eftc42omx6403001659.png (97 KB, 下載次數(shù): 2)
下載附件
保存到相冊
h5eftc42omx6403001659.png
2024-9-3 17:21 上傳
刻度方向和長短設(shè)置,其中刻度方向有:'in'|'out'|'both'?。figure('name','設(shè)置刻度方向、長短')w=WindRosePlot(data);w.AxisMainTickDir='both';w.AxisMinorTickDir='out';w.AxisColor=[1,0,0];w.AxisLineWidth=2;w.AxisTickLength=0.08;w.plot();
wwul5sv10pq6403001759.png (101.3 KB, 下載次數(shù): 3)
下載附件
保存到相冊
wwul5sv10pq6403001759.png
2024-9-3 17:21 上傳
網(wǎng)格設(shè)置:主、次網(wǎng)格的顏色、粗細(xì)、透明度設(shè)置。figure('name','主、次網(wǎng)格樣式、顏色、粗細(xì)、透明度')w=WindRosePlot(data);w.RGrid='on';w.RGridLineStyle='-.';w.RGridColor=[1,0,0];w.RGridAlpha=0.5;w.RGridLineWidth=1.5;
w.ThetaGrid='on';w.ThetaGridLineStyle='--';w.ThetaGridColor=[0,1,0];w.ThetaGridAlpha=0.5;w.ThetaGridLineWidth=2;
w.MinorRGrid='on';w.MinorRGridLineStyle='-';w.MinorRGridColor=[1,1,0];
w.MinorThetaGrid='on';w.MinorThetaGridLineStyle=':';w.MinorThetaGridColor=[0,0,1];w.plot();
uzsxfdflkzi6403001859.png (97.61 KB, 下載次數(shù): 3)
下載附件
保存到相冊
uzsxfdflkzi6403001859.png
2024-9-3 17:21 上傳
背景顏色設(shè)置figure('name','背景顏色')w=WindRosePlot(data);w.ColorMaps=hsv;w.AxisBackgroundColor=[1,1,1]*0.10;w.AxisFaceAlpha=0.1;w.plot();
1mhllylrktt6403001959.png (99.22 KB, 下載次數(shù): 0)
下載附件
保存到相冊
1mhllylrktt6403001959.png
2024-9-3 17:21 上傳
圖列設(shè)置figure('name','圖列設(shè)置:title設(shè)置、背景色、邊緣色、邊緣粗細(xì)、字體屬性')w=WindRosePlot(data);w.LegendTitle='圖列設(shè)置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();
krmh5zcxf4t6403002059.png (96.27 KB, 下載次數(shù): 3)
下載附件
保存到相冊
krmh5zcxf4t6403002059.png
2024-9-3 17:21 上傳
顏色條設(shè)置
figure('name','顏色條設(shè)置')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();
gyx4onevpdf6403002100.png (96.64 KB, 下載次數(shù): 2)
下載附件
保存到相冊
gyx4onevpdf6403002100.png
2024-9-3 17:21 上傳
|
gvnv0tesien6403002200.png (96.39 KB, 下載次數(shù): 0)
下載附件
保存到相冊
gvnv0tesien6403002200.png
2024-9-3 17:21 上傳
|
a1fceattvgz6403002300.png (96.68 KB, 下載次數(shù): 3)
下載附件
保存到相冊
a1fceattvgz6403002300.png
2024-9-3 17:21 上傳
|
c4fwcc4dh5y6403002400.png (96.49 KB, 下載次數(shù): 3)
下載附件
保存到相冊
c4fwcc4dh5y6403002400.png
2024-9-3 17:21 上傳
|
gg0pkbotp316403002500.png (95.73 KB, 下載次數(shù): 0)
下載附件
保存到相冊
gg0pkbotp316403002500.png
2024-9-3 17:21 上傳
|
| WindRosePlot類函數(shù)收藏=學(xué)會
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', {'風(fēng)向', '頻率', '最大風(fēng)速', '平均風(fēng)速'}); % data=[windDirections,windFrequency,maxWindSpeed,avgWindSpeed]; % w=WindRosePlot(data); % w.plot(); %------------------------------------------------- properties fig % WindDirections; %風(fēng)向 WindFrequency; %刮風(fēng)頻率 MaxWindSpeed; %最大風(fēng)速 AvgWindSpeed; %平均風(fēng)速 % RGrid='on'; %顯示主 r 軸網(wǎng)格線 ThetaGrid='on'; %顯示主 Theta 軸網(wǎng)格線 MinorRGrid='on'; %顯示次 r 軸網(wǎng)格線 MinorThetaGrid='on';%顯示次 Theta 軸網(wǎng)格線 % YLabel='風(fēng)向頻率(%)'; % R主網(wǎng)格樣式 RGridLineStyle='-'; RGridColor=[0,0,0]; RGridAlpha=1; RGridLineWidth=0.5; % Theta 主網(wǎng)格樣式 ThetaGridLineStyle='-'; ThetaGridColor=[0.8 0.8 0.8]; ThetaGridAlpha=1; ThetaGridLineWidth=1.2; % R次網(wǎng)格樣式 MinorRGridLineStyle=':'; MinorRGridColor=[0.55 0.55 0.55]; MinorRGridAlpha=1; MinorRGridLineWidth=0.8; % Theta 次網(wǎng)格樣式 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='最大風(fēng)速(m/s)'; ColorMapsFontName='Times new Roman'; ColorMapsFontSize=12; ColorMapsFontAngle='normal';%'normal' | 'italic'。 ColorMapsFontWeight='normal'; %'normal'|'bold' %legend 圖列 LegendTitle='平均風(fēng)速(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];%重新設(shè)定 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的極坐標(biāo)網(wǎng)格線=============================== % # 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的極坐標(biāo)網(wǎng)格線 % # 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 % ===================繪制刻度=============================== % 繪制最外層的極坐標(biāo)線 [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 % ==========添加主刻度外的標(biāo)簽=========================== labelDist = maxRadius *(1+ 0.094); % 設(shè)定標(biāo)簽距離為最大半徑(1+ 0.094),可以根據(jù)需要進(jìn)行調(diào)整 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 %==================添加數(shù)據(jù)======================== adjustedWindDirections = 360 - obj.WindDirections; [xWind, yWind] = pol2cart(deg2rad(adjustedWindDirections + 90), obj.WindFrequency); colorMapMajor = makeColorMap(obj.ColorMaps, length(obj.WindDirections)); % 根據(jù)風(fēng)速大小對顏色進(jìn)行排序 [~, idx] = sort(obj.MaxWindSpeed, 'Ascend'); sortedColorMap = colorMapMajor(idx, :); %對于相同的風(fēng)速值,使用第一個出現(xiàn)的顏色值為其賦色 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在正確的范圍內(nèi) 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:存儲圖形的句柄。
WindDirections:風(fēng)向數(shù)據(jù)。
WindFrequency:刮風(fēng)頻率。
MaxWindSpeed:最大風(fēng)速數(shù)據(jù)。
AvgWindSpeed:平均風(fēng)速數(shù)據(jù)。
RGrid,ThetaGrid,MinorRGrid,MinorThetaGrid:這些屬性用于控制是否顯示對應(yīng)的網(wǎng)格線。
YLabel:Y軸標(biāo)簽。
還有許多其他的屬性,例如RGridLineStyle,RGridColor等,用于控制圖形的樣式和外觀。
方法:構(gòu)造函數(shù)WindRosePlot:當(dāng)創(chuàng)建WindRosePlot類的對象時,此構(gòu)造函數(shù)將被調(diào)用。構(gòu)造函數(shù)接收風(fēng)玫瑰數(shù)據(jù)作為輸入,并初始化類的屬性。此外,還設(shè)置了默認(rèn)的圖形樣式。
plot方法:這個方法用于繪制風(fēng)玫瑰圖。它首先設(shè)置坐標(biāo)軸和極坐標(biāo)的屬性。接下來,它會繪制主要和次要的R和Theta網(wǎng)格線。然后,它繪制刻度。
[/ol]- -THE END- -
源碼下載:gitee下載:https://gitee.com/iDMatlab/wind-rose-plot
QQ 群下載
送書活動
包郵贈送 「北京大學(xué)出版社」贊助《AI時代程序員開發(fā)之道》和《AI時代項目經(jīng)理成長之道》本書《AI時代程序員開發(fā)之道》:知識精進(jìn)+重點解析+上機實訓(xùn)+綜合實戰(zhàn)+ChatGPT應(yīng)用,零基礎(chǔ)入門,讓程序員插上翅膀高效開發(fā)!本書《AI時代項目經(jīng)理成長之道》:1.開發(fā)新模式:讓項目管理更高效、更快捷、更完美。 2.全流程解析:涵蓋ChatGPT的不同應(yīng)用場景,從編寫各種文檔,到組建高效團隊、輔助項目溝通管理、計劃管理、時間管理、質(zhì)量管理等使用ChatGPT進(jìn)行程序開發(fā)的關(guān)鍵技巧。 3.實戰(zhàn)檢驗:ChatGPT結(jié)合多種項目管理工具及案例實操講解,理解更加透徹。 4.100%提高開發(fā)效率:揭秘ChatGPT與項目管理工作高效融合的核心方法論和實踐經(jīng)驗。 5.超值資源:免費贈送全書案例源文件、教學(xué)視頻及配套工具,供讀者下載學(xué)習(xí)。
【抽獎方式及滿足條件】:
1.關(guān)注「好玩的MATLAB 」公眾號和視頻號
2.給本文點【贊】+【在看】;
3.留言區(qū)評論點贊最多的前3名。
4.本活動只針對從未獲過獎的同學(xué),之前獲過獎的小伙伴,不用參加。
同時滿足上述4個條件的讀者朋友,包郵贈送一本書。
【開獎時間】:2023年10月31日夜晚8點
【領(lǐng)獎方式】:在開獎時加小編私人微信:idmatlab
掃一掃加管理員微信
|