• 初阶绘图


    %%
    %plot
    plot(cos(0:pi/20:2*pi))
    plot(0:pi/20:2*pi,cos(0:pi/20:2*pi))
    %从上面可以发现matlab后一次画的图会覆盖前一次画的图
    %%
    %hold on
    %我们可以使用hold on命令来保留上一次画的图不被擦掉
    hold on
    plot(cos(0:pi/20:2*pi))
    plot(sin(0:pi/20:2*pi),'xg')
    hold off
    %%
    %当然也可以一个plot画很多个图也是可以的
    plot(0:pi/20:2*pi,cos(0:pi/20:2*pi),0:pi/20:2*pi,sin(0:pi/20:2*pi))
    %我们还可以使用legend对线段进行标记
    legend('cos(x)','sin(x)');
    %此时图像还是不够完整我们还有坐标轴没有标注和图的名称没有标注
    %使用title(),xlabel(),ylabel(),zlabel()对图像进行标注
    xlabel('t=0 to 2pi');
    ylabel('values of sin(t) and cos(t)');
    title('Function Plots of sin(t) and cos(t)')
    %%
    %text()和annotation(),text()是用来画字符的,annotation是画箭头的
    x=linspace(0,3);y=x.^2.*sin(x);plot(x,y);
    line([2,2],[0,2^2*sin(2)]);
    str='$$int_{0}^{2}x^2sin(x) dx $$'
    text(0.25,2.5,str,'Interpreter','latex');
    annotation('arrow',[0.32,0.5],[0.6,0.4]);
    %%
    %获得所绘制图形的参数
    x=linspace(0,2*pi,1000);
    y=sin(x);plot(x,y);
    h=plot(x,y);get(h)
    %如果执行get(gca)则显示出来的是所绘图形坐标轴的参数
    %Setting Axes Limits
    set(gca,'XLim',[0,2*pi]);
    set(gca,'YLim',[-1.2,1.2]);
    set(gca,'FontSize',25);
    set(gca,'XTick',0:pi/2:2*pi);%设置有几个tick并且tick的值为多少
    set(gca,'XTickLabel',0:90:360);%对每一个tick的label进行修改
    %或者我们可以更加直观一些将x轴的tick标为pi,但是这里首先需要我们将类型转换一下,转换成字符串
    set(gca,'FontName','Latex');
    set(gca,'XTickLabel',{'0','pi/2','pi','3pi/2','2pi'});
    %Line Specification设置所画线段的参数
    set(h,'LineStyle','-.','LineWidth',7.0,'Color','g')
    %%
    %一个小test
    x=rand(20,1);set(gca,'FontSize',18);
    plot(x,'-dm','Linewidth',2,'MarkerEdgeColor','k',...
        'MarkerFaceColor','g','MarkerSize',10);
    xlim([1,20]);
    %%
    %subplot
    t=0:0.1:2*pi;x=3*cos(t);y=sin(t);
    subplot(2,2,1);plot(x,y);axis normal
    subplot(2,2,2);plot(x,y);axis square
    subplot(2,2,3);plot(x,y);axis equal
    subplot(2,2,4);plot(x,y);axis equal tight
    %Saving Figures into Files
    %saveas或者printf函数
    
  • 相关阅读:
    LCA + 二分(倍增)
    Educational Codeforces Round 5
    BNU 51276
    POJ 1511
    hdu2121
    最小树形图(朱刘算法)
    Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)
    Educational Codeforces Round 1(C. Nearest vectors)
    POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)
    POJ 1661Help Jimmy(逆向DP Or 记忆化搜索 Or 最短路径)
  • 原文地址:https://www.cnblogs.com/mudrobot/p/15201162.html
Copyright © 2020-2023  润新知