【问题标题】:Plotting a phase portrait with multiple colors with MATLAB使用 MATLAB 绘制具有多种颜色的相图
【发布时间】:2018-11-06 03:27:14
【问题描述】:

我想添加一些内容以使我的相图更易于理解。不过,我什么也找不到(我找到了这个 https://se.mathworks.com/help/matlab/ref/colorspec.html https://se.mathworks.com/matlabcentral/fileexchange/11611-linear-2d-plot-with-rainbow-color https://se.mathworks.com/help/symbolic/mupad_ref/linecolortype.html ) 但这不是我需要的。

我真的很想看到相像线的颜色根据它是在模拟开始还是在模拟结束时发生变化。

我发现这个想法看起来很棒:

我完全不明白他做了什么(我想代码写在这里: https://blogs.mathworks.com/pick/2008/08/15/colors-for-your-multi-line-plots/) 但是,如果我可以绘制一个颜色随时间变化的单线函数,那就太好了。此外,如果像图片上的那样,我可以在右边有一个刻度:那就太棒了。

所以现在,我有那个:

data = readtable('test.txt');
figure('Name','Phase'  , 'units','normalized','outerposition',[(8/100) (0.3- 16/100) 0.5 0.7]);
hold on
   plot(data{:,2},data{:,3}, 'k.', 'LineWidth',1.5 );
   plot(data{:,4},data{:,5}, 'r.', 'LineWidth',1.5  );
xL = xlim;
yL = ylim;
line([0 0], yL);  %x-axis
line(xL, [0 0]);  %y-axis   
      title(['Phase portrait'])
      xlabel('f')
      ylabel('f '' ')
hold off 

我在 .txt 文件中读取函数的值,然后绘制第 2/3 列和第 4/5 列。第一列是时间演变。

你有什么想法吗:)?

谢谢!

【问题讨论】:

    标签: matlab plot colors


    【解决方案1】:

    说实话,有几种方法可以解决这个问题。 但是,如果您让我们知道您的时间数据是什么,它会更容易一些。 您是在 x(或 y)轴上绘制时间数据还是不同的附加数据集。如果它是一个附加数据集,那么您可以将其视为 z 数据,绘制在 Z 轴上或/和作为颜色。 下面是一个示例,您可以通过制作 3D 绘图但以 2D 显示它,这使您可以毫无问题地添加颜色条。

    x=0:5;
    y=0:5;
    z=rand(1,6); %random data to simulate your time
    xx=[x' x']; %this allows you to plot the data using surf in 3d
    yy=[y' y']; %same as for xx
    z1=zeros(size(xx)); % we don't need z-data so we're making it a matrix of zeros
    zc=[z' z']; %input here your time data values, if x/y then you can just use those instead of z
    hs=surf(xx,yy,z1,zc,'EdgeColor','interp') %// color binded to "z" values, choose interp for interpolated/gradual color changes, flat makes it sudden
    colormap('hsv') %choose your colormap or make it yourself
    view(2) %// view(0,90)
    hcb=colorbar; %add a colorbar
    

    【讨论】:

    • 嗨!感谢您的回答。我试图调整我的代码,但它告诉我 Z 是矩阵而不是向量。为了回答您的问题,我认为您的建议是解决我的问题的好方法。时间将是 .txt 文件夹中的第一个 collum。可以用您的解决方案完成吗?我试着写这样的东西:x=data{:,2}; y=数据{:,3};
    • 是的,但如果我不知道您的数据是什么样的,很难说该怎么做。它可能与您需要如何在 surf 函数中绘制 z1 和 zc 有关,因为这些就像 xx 和 yy 需要是矩阵而不是向量一样,否则您不能使用 surf。此外,xx、yy、z1 和 zc 都需要具有相同的大小(形状)。可能是您可以直接输入时间数据,而不是将其转换为矩阵(如果它已经是矩阵)。
    • 那么你的数据是什么形状的?
    【解决方案2】:

    感谢 stackoverflaw 上的另一个用户,我找到了这个。

        data = readtable('4ressorspendule.txt');
    n = numel(data.Var1);
    c = size(data,2);
    
    figure('Name','Phase'  , 'units','normalized','outerposition',[(8/100) (0.3 - 16/100) 0.5 0.7]);
    for i=1:n
        hold on
        plot(data{i,2},data{i,3},'.','Color',[1 (1-i/n) 0] ,'MarkerSize',4);  
        plot(data{i,4},data{i,5},'.','Color',[0 (i/n) (1-i/n)],'MarkerSize',4);
    end
    xL = xlim;
    yL = ylim;
    line([0 0], yL);  %x-axis
    line(xL, [0 0]);  %y-axis   
    title(['Phase portrait'])
    xlabel('f')
    ylabel('f '' ')
    hold off 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 2013-05-05
      相关资源
      最近更新 更多