MATLAB | MATLAB不会画图?官方团队来教你

Posted slandarer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MATLAB | MATLAB不会画图?官方团队来教你相关的知识,希望对你有一定的参考价值。

让我看看是哪个小傻瓜还没用过MATLAB官方gallery,常见的图直接MATHWORKS搜索一下就能找到,一些有意思的组合图,以及一些特殊属性的设置MATHWORKS官方是有专门去整理的,虽然一些很特殊的图还是没有(哈哈哈弦图小提琴图啥的官方没有的我自己大部分都写过补充过),但是也依旧足够收获很多了!!

MATLAB Plot Gallery

地址:
https://ww2.mathworks.cn/products/matlab/plot-gallery.html?s_tid=srchtitle_gallery_1

其中有超超超多优秀绘图案例:

点击launch example甚至可以在线运行例子,优秀!

点击左侧download code可以下载全部代码及数据:

有的程序运行时会提示你没有数据,你下载的文件包内就有一个名为Data Sets的文件夹。把文件夹里的mat文件复制过去即可:

这里随便两个例子运行:

load BostonTemp.mat
yearIdx = 13;      % Choose the starting year to visualize the monthly temperature for five years.
TempData5Years = Temperatures(yearIdx:yearIdx+4,:);
barWidth = 0.5;
figure
b = bar3(TempData5Years,barWidth);      % Specify bar width in the third argument

for k = 1:length(b)
    zdata = b(k).ZData;                 % Use ZData property to create color gradient
    b(k).CData = zdata;                 % Set CData property to Zdata
    b(k).FaceColor = "interp";          % Set the FaceColor to 'interp' to enable the gradient 
end
title(sprintf("Average Monthly Temperatures from %d to %d",Year(yearIdx),Year(yearIdx+4)))
xlabel("Month")
ylabel("Year")
zlabel("Temperature (\\circF)")

xticklabels(Months)
yticklabels(Year(yearIdx):Year(yearIdx+4))

box on

load("rideData.mat")

faceColorType = "flat";
h2 = histogram2(rideData.Duration, rideData.birth_date,...
   "FaceColor",faceColorType);                                          % Specify the bar color scheme

title("Ride counts based on ride length and the age of the rider")
xlabel("Length of Ride")
ylabel("Birth Year")
zlabel("Number of Rides")
view(17,30)

colormap("turbo"); % Specify colormap

[r,theta,x,y,streamline,pressure] = flowAroundCylinder();

contourLevels = 20; 
LineWidth = 1; 

[~,c] = contourf(x,y,pressure,...
    contourLevels,...              % Specify a scalar integer number of contour levels
    "LineWidth",LineWidth);        % Specify the contour line width

axis([-5, 5,...     % x-axis limits 
      -5, 5]);      % y-axis limits 
circle(0,0,1);      % Call helper function to plot circle

xlabel("x/R")
ylabel("y/R")
title("Flow pressure over cylinder")

set(gca,...
    "FontSize",15,...           % Set font size
    "FontAngle","italic");      % Italicize font

colormap("turbo");    % Specify a colormap to use in the contourf plot
cb = colorbar;                  
cb.Ticks = cb.Limits;                    
cb.TickLabels = ["High" "Low"];  % Specify labels for colorbar

function [r,theta,x,y,streamline,pressure] = flowAroundCylinder()
V_i = 1000;
a = 1;
theta = linspace(0,2*pi,100); 
rr = linspace(a,10*a,100);        
[t,r] = meshgrid(theta,rr);                        % create meshgrid in two dimensions
[x,y] = pol2cart(t,r);                             % converts polar to cartesian coordinates
streamline = V_i.*sin(t).*r.*(1-(a^2./(r.^2)));      % Creation of the streamline function
pressure = 2*(a.^2./r.^2).*cos(2.*t)-(a.^4./r.^4); % static pressure around the cylinder
end

function h = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit,"-k","LineWidth",2);
hold off
end

MathWorks Plot Gallery Team

上面那些学完了没学够怎么办??MATHWORKS官方团队MathWorks Plot Gallery Team还在fileexchange上上传了大量例子:

地址:
https://ww2.mathworks.cn/matlabcentral/profile/authors/3166380

依旧有非常多优秀例子:

随便点开一个再点击右侧下载即可:

下载完直接就可以运行,以下依旧举几个例子:

%%
% *This is an example of creating area charts, bar charts, and pie charts with some annotation in MATLAB®* .
% 
% You can open this example in the <https://www.mathworks.com/products/matlab/live-editor.html 
% Live Editor> with MATLAB version 2016a or higher.
%
% Read about the <http://www.mathworks.com/help/matlab/ref/fill.html |fill|>, <http://www.mathworks.com/help/matlab/ref/bar.html |bar|>, <http://www.mathworks.com/help/matlab/ref/text.html |text|>, and <http://www.mathworks.com/help/matlab/ref/pie.html |pie|> functions in the MATLAB documentation.
% For more examples, go to <http://www.mathworks.com/discovery/gallery.html MATLAB Plot Gallery>
%
% Copyright 2012-2018 The MathWorks, Inc.

% Set up data
t  = 0:0.01:2*pi;
x1 = -pi/2:0.01:pi/2;
x2 = -pi/2:0.01:pi/2;
y1 = sin(2*x1);
y2 = 0.5*tan(0.8*x2);
y3 = -0.7*tan(0.8*x2);
rho = 1 + 0.5*sin(7*t).*cos(3*t);
x = rho.*cos(t);
y = rho.*sin(t);

% Create the left plot (filled plots, errorbars, texts)
figure
subplot(121)
hold on
h(1) = fill(x, y, [0 .7 .7]);
set(h(1), 'EdgeColor', 'none')

h(2) = fill([x1, x2(end:-1:1)], [y1, y2(end:-1:1)], [.8 .8 .6]);
set(h(2), 'EdgeColor', 'none')

h(3) = line(x1, y1, 'LineWidth', 1.5, 'LineStyle', ':');
h(4) = line(x2, y2, 'Linewidth', 1.5, 'LineStyle', '--', 'Color', 'red');
h(5) = line(x2, y3, 'Linewidth', 1.5, 'LineStyle', '-.', 'Color', [0 .5 0]);

% Create error bars
err = abs(y2-y1);
hh = errorbar(x2(1:15:end), y3(1:15:end), err(1:15:end), 'r');
h(6) = hh(1);

% Create annotations
text(x2(15), y3(15), '\\leftarrow \\psi = -.7tan(.8\\theta)', ...
   'FontWeight', 'bold', 'FontName', 'times-roman', ...
   'Color', [0 0.5 0], 'FontAngle', 'italic')
text(x2(10), y2(10),'\\leftarrow \\psi = .5tan(.8\\theta)', ...
   'FontWeight', 'bold', 'FontName', 'times-roman',...
   'Color', 'red', 'FontAngle', 'italic')

text(0, -1.65, 'Text box', 'EdgeColor', [.3 0 .3], ...
   'HorizontalAlignment', 'center', ...
   'VerticalAlignment', 'middle', 'LineStyle', ':', ...
   'FontName', 'palatino', 'Margin', 4, 'BackgroundColor', [.8 .8 1], ...
   'LineWidth', 1)

% Adjust axes properties
axis equal
set(gca, 'Box', 'on', 'LineWidth', 1, 'Layer', 'top', ...
   'XMinorTick', 'on', 'YMinorTick', 'on', 'XGrid', 'off', 'YGrid', 'on', ...
   'TickDir', 'out', 'TickLength', [.015 .015], 'XLim', x1([1,end]),...
   'FontName', 'avantgarde', 'FontSize', 10, 'FontWeight', 'normal', ...
   'FontAngle', 'italic')

xlabel('theta (\\theta)', 'FontName', 'bookman', 'FontSize', 12, ...
   'FontWeight', 'bold')
ylabel('value(\\Psi)', 'FontName', 'helvetica', 'FontSize', 12, ...
   'FontWeight', 'bold', 'FontAngle', 'normal')
title('Cool Plot', 'FontName','palatino', 'FontSize', 18, ...
   'FontWeight', 'bold', 'FontAngle', 'italic', 'Color', [.3 .3 0])
legh = legend(h, 'blob', 'diff', 'sin(2\\theta)', 'tan', 'tan2', 'error');
set(legh, 'FontName', 'helvetica', 'FontSize', 8, 'FontAngle', 'italic')

% Create the upper right plot (bar chart)
subplot(222)
bar(rand(10,5), 'stacked')
set(gca, 'Box', 'on', 'LineWidth', .5, 'Layer', 'top', ...
   'XMinorTick', 'on', 'YMinorTick', 'on', 'XGrid', 'on', 'YGrid', 'on', ...
   'TickDir', 'in', 'TickLength', [.015 .015], 'XLim', [0 11], ...
   'FontName', 'helvetica', 'FontSize', 8, 'FontWeight', 'normal', ...
   'YAxisLocation', 'right')
xlabel('bins', 'FontName', 'avantgarde', 'FontSize', 10, ...
   'FontWeight', 'normal')
yH = ylabel('y val (\\xi)', 'FontName', 'bookman', 'FontSize', 10, ...
   'FontWeight', 'normal');
set(yH, 'Rotation', -90, 'VerticalAlignment', 'bottom')
title('Bar Graph', 'FontName', 'times-roman', 'FontSize', 12, ...
   'FontWeight', 'bold', 'Color', [0 .7 .7])

% Create the bottom right plot (pie chart)
subplot(224)
pie([2 4 3 5], 'North', 'South', 'East', 'West')
tP = get(get(gca, 'Title'), 'Position');
set(get(gca, 'Title'), 'Position', [tP(1), 1.2, tP(3)])
title('Pie Chart', 'FontName', 'avantgarde', 'FontSize', 12, ...
   'FontWeight', 'bold', 'FontAngle', 'italic

以上是关于MATLAB | MATLAB不会画图?官方团队来教你的主要内容,如果未能解决你的问题,请参考以下文章

Matlab画图怎么把横坐标设成文字

matlab画图如何清除图中的一部分

如何用matlab求解微分方程并画图

matlab画图,如何修改横坐标的显示数值,急

MATLAB与Matplotlib画图速查表

MATLAB与Matplotlib画图速查表