【问题标题】:MATLAB: Plot line colorMATLAB:绘制线条颜色
【发布时间】:2018-07-17 01:15:07
【问题描述】:

我正在尝试运行以下函数,以便绘制一些由线连接的点,但这些线没有以我想要的颜色(白色)显示。我也尝试了其他颜色,但线条仍然不显示。我不明白为什么。有人可以帮忙吗?

    function draw_constellations

    figure
    hold on
    axis([0,100,0,100])
    grid off
    set(gcf,'color','k')
    set(gca,'color','k')
    axis square
    while 1
        [x,y,button] = ginput(1);
        switch button
            case 1 % left mouse
                p = plot(x,y,'w-*');
            case {'q','Q'} % on keyboard
                break;
        end
    end

【问题讨论】:

    标签: matlab plot


    【解决方案1】:

    这是因为您以前绘制的x 和y 是缩放器。要绘制线条,您需要将从ginput 获得的所有x 和y 存储在向量中并绘制向量:

    [x,y,button] = ginput(1);
    switch button
        case 1 % left mouse
            xs = [xs x];
            ys = [ys y];
            p = plot(xs,ys,'w-*');
            % more code to go
    end
    

    但是,如果在每次输入新点时都绘制 xs 和 ys,则线条会重叠。为避免这种情况,我们只绘制第一个点并更新 p.XData 和 p.YData 以获得新点:

    if isempty(p)
        p = plot(x,y,'w-*');
    else
        p.XData = xs;
        p.YData = ys;
    

    完整代码:

    figure
    hold on
    axis([0,100,0,100])
    grid off
    set(gcf,'color','k')
    set(gca,'color','k')
    axis square
    
    xs = [];
    ys = [];
    p = [];
    
    while 1
        [x,y,button] = ginput(1);
        switch button
            case 1 % left mouse
                xs = [xs x];
                ys = [ys y];
                if isempty(p)
                    p = plot(x,y,'w-*');
                else
                    p.XData = xs;
                    p.YData = ys;
                end
            case {'q','Q'} % on keyboard
                break;
        end
    end
    

    结果:

    【讨论】:

      【解决方案2】:

      你可以使用

      plot(x,y,'*','color','blue') % plots in blue. 
      plot(x,y,'*','color',[.5 .4 .7]) % plots the RGB value [.5 .4 .7]. 
      

      如果您想要很多颜色名称,您可以使用 rgb 函数返回几乎任何颜色的 RGB 值。例如,

      plot(x,y,'*','color',rgb('blood red'))
      

      进一步演示如何更改在不指定颜色的情况下绘制线条时获得的默认颜色顺序:

      有没有想过它是如何先绘制蓝色,然后是深绿色,然后是红色,然后是青色等等?曾经想要更改默认顺序,以便它使用您想要的颜色顺序而不是默认颜色顺序绘制曲线,并且不必在每次调用 plot() 时指定颜色?如果是这样,请运行随附的演示。

      % Unless you specify the 'Color' property when you plot,
      % plots are plotted according to the 'ColorOrder' property of the axes.
      % This demo shows how you can change the default color order of plots.
      
      clc;    % Clear the command window.
      close all;  % Close all figures (except those of imtool.)
      clear;  % Erase all existing variables.
      workspace;  % Make sure the workspace panel is showing.
      fontSize = 18;
      
      % Make 20 plots, with 25 data points in each plot.
      numberOfDataSets = 20;
      x = 1:25;
      y = rand(numberOfDataSets, length(x));
      % These y would all be on top of each other.
      % Separate the plots vertically.
      offsets = repmat((1:numberOfDataSets)', [1, length(x)]);
      y = y + offsets; 
      
      % Get the initial set of default plot colors.
      initialColorOrder = get(gca,'ColorOrder') % Initial
      
      % See what the colors look like when plotted:
      subplot(2, 1, 1);
      plot(x,y, 'LineWidth', 3);
      grid on;
      caption = sprintf('%d plots with the Initial Default Color Order (Note the repeating colors)', numberOfDataSets);
         title(caption, 'FontSize', fontSize);
      xlabel('X', 'FontSize', fontSize);
      ylabel('Y', 'FontSize', fontSize);
      
      % Enlarge figure to full screen.
      set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
      % Give a name to the title bar.
      set(gcf,'name','Image Analysis Demo','numbertitle','off')
      
      choice = menu('Which ColorOrder do you want?', 'jet', 'random', 'hsv', 'hot', 'cool', 'spring', 'summer',...
      'autumn', 'winter', 'lines', 'gray', 'bone', 'copper', 'pink');
      
      % Make a new axes:
      subplot(2, 1, 2);
      
      % Create a new colormap that will define the new default color order property.
      switch choice
          case 1
              newDefaultColors = jet(numberOfDataSets);
          case 2
              newDefaultColors = rand(numberOfDataSets, 3);
          case 3
              newDefaultColors = hsv(numberOfDataSets);
          case 4
              newDefaultColors = hot(numberOfDataSets);
          case 5
              newDefaultColors = cool(numberOfDataSets);
          case 6
              newDefaultColors = spring(numberOfDataSets);
          case 7
              newDefaultColors = summer(numberOfDataSets);
          case 8
              newDefaultColors = autumn(numberOfDataSets);
          case 9
              newDefaultColors = winter(numberOfDataSets);
          case 10
              newDefaultColors = lines(numberOfDataSets);
          case 11
              newDefaultColors = gray(numberOfDataSets);
          case 12
              newDefaultColors = bone(numberOfDataSets);
          case 13
              newDefaultColors = copper(numberOfDataSets);
          otherwise
              newDefaultColors = pink(numberOfDataSets);
      end
      % Note: You can build your own custom order if you want, 
      % just make up a array with numberOfDataSets rows and 3 columns
      % where each element is in the range 0-1.
      
      % Apply the new default colors to the current axes.
      set(gca, 'ColorOrder', newDefaultColors, 'NextPlot', 'replacechildren');
      
      % Now get the new set of default plot colors.
      % Verify it changed by printing out the new default color set to the commandwindow.
      newColorOrder = get(gca,'ColorOrder')
      
      % Now plot the datasets with the changed default colors.
      plot(x,y, 'LineWidth', 3);
      grid on;
      caption = sprintf('%d plots with the New Default Color Order', numberOfDataSets);
      title(caption, 'FontSize', fontSize);
      xlabel('X', 'FontSize', fontSize);
      ylabel('Y', 'FontSize', fontSize);
      
      msgbox('Done with ColorOrder demo!');    
      

      特别感谢/感谢https://ch.mathworks.com/matlabcentral/answers/uploaded_files/16843/ColorOrder_demo.m

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-23
        • 1970-01-01
        • 2012-11-05
        相关资源
        最近更新 更多