【问题标题】:Merge key entries in gnuplot合并 gnuplot 中的关键条目
【发布时间】:2017-05-16 08:55:21
【问题描述】:

我想用不同颜色的线和点绘制数据。似乎存在不同的解决方案:

但是,它们都没有正确处理密钥,只显示一个条目,其中包含线条和不同颜色的点... 还有其他方法可以实现吗?

这是最小(非)工作示例。

set key bottom right
plot '+' using 1:1 title "same data with different lines and points color" with lines lc 'blue', '' using 1:1 every 3 notitle with points ps 1.2 pt 7 lc 'red';

亲切的问候, 亚历克西斯。

【问题讨论】:

    标签: gnuplot


    【解决方案1】:

    如果您的情节不是太复杂,您也许可以通过使用图例的spacing 参数来实现这一点。将spacing 设置为 -1 可实现标签/符号重叠:

    set terminal pngcairo enhanced
    set output 'fig.png'
    
    set xr [-pi/2:pi/2]
    set yr [0:1]
    
    set key at graph 0.95,graph 0.9 spacing -1
    
    plot \
        cos(x) w l lc rgb 'dark-red' lw 2, \
        '-' w p lc rgb 'royalblue' pt 7 ps 2 t ' '
    -1  0.540302
    0   1
    1   0.540302
    e
    

    这给了

    但是,缺点是设置在某种意义上是全局的 - 如果绘图包含更多函数/数据文件,所有内容都会重叠。为了在这种特殊情况下使用这个“方法”,有必要调用multiplot 并单独创建密钥。

    【讨论】:

      【解决方案2】:

      感谢 ewcz 的 answer,这是朝着预期结果迈出的第一步。然而,正如你所说,如果你有多个函数/数据要显示在同一个图上,这会有点棘手。

      下面是一个最小的工作示例,其中包含两个函数(因此,两个关键条目),带有不同颜色的线、点和点轮廓。

      # These parameters are used to compute the spacing between entries of the key
      pointSize = 1;
      yticsScale = 1;
      # We use the default spacing (1.25)
      keySpacing = pointSize*yticsScale*1.25;
      
      # Initial coordinate of the key
      keyY = 4; # In character system
      keyX = 0.87; # In graph system
      
      # Just to generate data
      set samples 20;
      set xrange [-pi:pi];
      
      set term pngcairo;
      set output 'graph.png';
      set xlabel "x"
      set ylabel "y"
      
      # Set the alignment (and thus the coordinate point) of the key
      # Set the spacing to -1 to stack different (thanks to ewcz for the idea)
      set key bottom right spacing -1
      
      # Start a multiplot
      set multiplot
      # Make plots as big as possible
      set origin 0,0
      set size 1,1
      
      # Set the key position
      set key at graph keyX, character keyY
      
      # Plot multiple times the same function with different styles.
      # Make sure that all functions have a title (empty if necessary).
      plot cos(x+pi) w l lc "light-red", \
           cos(x+pi) w p pt 5 ps 1.8 lc "dark-red" t ' ', \
           cos(x+pi) w p pt 5 ps 1.2 lc "red" t ' '
      # Update key coordinates for the next plot
      keyY = keyY + keySpacing
      
      # Draw the key of the next plot at the new coordinates
      set key at graph keyX, character keyY
      plot cos(x) w l lc "light-blue", \
           cos(x) w p pt 7 ps 1.8 lc "dark-blue" t ' ', \
           cos(x) w p pt 7 ps 1.2 lc "blue" t ' ';
      
      # That's all
      unset multiplot
      set output;
      

      结果图:

      希望对其他人有所帮助。

      亲切的问候。

      亚历克西斯


      编辑:

      如果两个函数/数据具有相同的范围(在 x 和 y 上)允许自动缩放正常工作,则前面的代码有效。

      对于不知道范围的数据,必须在绘制之前计算它。

      # Just to generate data
      set samples 20;
      
      # First data will be defined on [-pi:pi] with values between -1 and 1.
      set table '1.dat'
      plot [-pi:pi] cos(x)
      unset table
      # Second data will be defined on [-pi/2,pi/2] with values between 0 and -2
      set table '2.dat'
      plot [-pi/2:pi/2] 2*cos(x+pi)
      unset table
      
      # These parameters are used to compute the spacing between entries of the key
      pointSize = 1;
      yticsScale = 1;
      keySpacingScale = 1.25; # Gnuplot default spacing
      keySpacing = pointSize * yticsScale * keySpacingScale; # Spacing computation
      
      set pointsize pointSize;
      set ytics scale yticsScale;
      set key spacing -1; # Make key entries overlapping (thanks ewcz for the idea)
      
      # Initial coordinate of the key
      keyY = 4.5; # In character system
      keyX = 0.98; # In graph system
      
      set term pngcairo;
      set output 'graph.png';
      
      # Remove redundant objects
      # Borders, labels, tics will be drawn for each plot, this is not necessary as all plots will be stacked. So remove then.
      set border 0
      set tics textcolor "white" # Dirty tricks to keep plots aligned but to not show the tics
      set xlabel " " # The same
      set ylabel " " # The same
      
      # Compute the ranges
      min(v1, v2) = (v1 < v2) ? v1 : v2;
      max(v1, v2) = (v1 > v2) ? v1 : v2;
      # Get min and max for the data
      stats [*:*] [*:*] '1.dat' name 'f1' nooutput;
      stats [*:*] [*:*] '2.dat' name 'f2' nooutput;
      # Get the range limits
      xmin = min(f1_min_x, f2_min_x)
      xmax = max(f1_max_x, f2_max_x)
      ymin = min(f1_min_y, f2_min_y)
      ymax = max(f1_max_y, f2_max_y)
      # Autoscale the range to match all the data
      set xrange [* < xmin:xmax < *] writeback
      set yrange [* < ymin:ymax < *] writeback
      
      # Start a multiplot
      set multiplot
      # Make plots as big as possible
      set origin 0,0
      set size 1,1
      
      # Set the key
      set key bottom right at graph keyX, character keyY
      
      # Plot multiple times the same function with different styles.
      # Make sure that all functions have a title (empty if necessary).
      plot '1.dat' w l lc "light-red" t "cos(x)", \
                '' w p pt 5 ps 1.8 lc "dark-red" t ' ', \
                '' w p pt 5 ps 1.2 lc "red" t ' '
      
      # Update key coordinates for the next plot
      keyY = keyY + keySpacing
      # Draw the key of the next plot at the new coordinates
      set key at graph keyX, character keyY
      
      # Display at least once the labels
      set border
      set tics textcolor "black"
      set xlabel "x"
      set ylabel "y"
      
      # Disable ranges autoscaling
      set xrange restore
      set yrange restore
      
      plot '2.dat' w l lc "light-blue" t "2cos(x+pi)", \
                '' w p pt 5 ps 1.8 lc "dark-blue" t ' ', \
                '' w p pt 5 ps 1.2 lc "blue" t ' '
      
      # That's all
      unset multiplot
      set output;
      

      再绘制一次结果图:

      亲切的问候,

      亚历克西斯

      【讨论】:

        猜你喜欢
        • 2011-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-13
        • 1970-01-01
        • 1970-01-01
        • 2018-06-17
        • 2019-04-15
        相关资源
        最近更新 更多