Menu

#507 plot style to draw rectangular meshes

None
closed-out-of-date
nobody
None
5
2022-01-15
2020-08-07
No

It would be nice to have a plot style that draws rectangular meshes according to the input data. With this plot style, it is much easier to prepare data for the following drawing.

  • Plotting 2D unequal mesh data
  • Plotting sparse mesh on a 2D grid
  • Display the ROI of object recognition

Alternatively, we can use set pm3d map and plot ... with pm3d to draw a 2d mesh plot, but it is difficult to assign precise color by value for each mesh and not to paint meshes corresponding to missing values.
It is the reason why I request this feature here to make to draw such plots easier.

plot "foo.dat" using 1:2:3:4 with rectangles     ### stroke rectangle with line
plot "foo.dat" using 1:2:3:4:5 with rectangles  ### fill rectangle with color

The sequence of the input data for this plot style could be x1:y1:x2:y2 or x1:y1:x2:y2:value. The rectangular mesh to be drawn is specified by (x1,y1) and (x2,y2) as diagonals and is colored by the value if it is given.

Discussion

  • Ethan Merritt

    Ethan Merritt - 2020-08-07

    Have you looked at the new plot style with polygons?

    gnuplot> help with polygons
    
          plot DATA {using 1:2} with polygons
    
     `plot with polygons` is currently treated as `plot with filledcurves closed`
    
          splot DATA {using x:y:z} with polygons
                {fillstyle <fillstyle spec>}
                {fillcolor <colorspec>}
     `splot with polygons` uses pm3d to render individual triangles, quadrangles,
     and larger polygons in 3D.  These may be facets of a 3D surface or isolated
     shapes. The code assumes that the vertices lie in a plane.
     Vertices defining individual polygons are read from successive records of the
     input file.  A blank line separates one polygon from the next.
    
     The fill style and color may be specified in the splot command, otherwise the
     global fillstyle from `set style fill` is used.  Due to limitations in the
     pm3d code, a single border line style from `set pm3d border` is applied to all
     polygons.  This restriction may be removed in a later gnuplot version.
    
     pm3d sort order and lighting are applied to the faces. It is probably always
     desirable to use `set pm3d depthorder`.
    
     
  • Hiroki Motoyoshi

    Thank you for the introduction to the plotting style 'with polygon'. However, it seems to be different from what I'm looking for. I'm sorry for my lack of explanation.

    First, let me explain the figure I'd most like to draw. In short, it is an "unequally spaced 2D frequency distribution with unfilled meshes partially". For a simplified example, look at the attachment "image-rectangles.png". The mesh represented by diagonals (1,1)-(3,3) in the figure is assumed not to be filled because of the NaN value. The following script generates this figure.

    set terminal pngcairo size 400,400
    set output "image-rectangles.png"
    
    set palette maxcolors 5
    set style fill transparent solid 0.9 noborder
    set xrange [0:6]
    set yrange [0:6]
    set cbrange [0:5]
    set size ratio 1
    unset key
    
    set object rectangle from 0,0 to 1,1 fill noborder fc palette cb 0 back 
    set object rectangle from 1,0 to 3,1 fill noborder fc palette cb 1 back 
    set object rectangle from 3,0 to 6,1 fill noborder fc palette cb 2 back 
    set object rectangle from 0,1 to 1,3 fill noborder fc palette cb 1 back 
    set object rectangle from 1,1 to 3,3 fill noborder fc palette cb NaN back 
    set object rectangle from 3,1 to 6,3 fill noborder fc palette cb 3 back 
    set object rectangle from 0,3 to 1,6 fill noborder fc palette cb 2 back 
    set object rectangle from 1,3 to 3,6 fill noborder fc palette cb 3 back 
    set object rectangle from 3,3 to 6,6 fill noborder fc palette cb 4 back 
    
    plot 4/x with lines lw 3 lc rgb 'black'
    

    The figure is drawn by the enumerates of the command 'set object rectangle ...'. This figure is exactly the kind of figure we want, but we need to embed a lot of 'set object rectangle ...' sentences in our script. For me, this is just a workaround to the straightforward approach because it's more like drawing of objects than plotting of data.

    If we don't have any NaN value for meshes, we can plot a similar figure with the combination of 'set pm3d map corners2color c1' and 'plot ... with pm3d'.

    set palette maxcolors 5
    set xrange [0:6]
    set yrange [0:6]
    set cbrange [0:5]
    set size ratio 1
    unset key
    set pm3d map corners2color c1
    set style fill transparent solid 0.9 noborder
    set parametric
    set urange [0:6]
    splot "-" using 1:2:3 with pm3d, u, 4/u, 1 with lines lw 3 lc rgb 'black'
    0 0 0
    1 0 1
    3 0 2
    6 0 3 # 3rd column '3' is dummuy value not used
    
    0 1 1
    1 1 2 
    3 1 3
    6 1 4 # 3rd column '4' is dummuy value not used
    
    0 3 2
    1 3 3
    3 3 4
    6 3 5 # 3rd column '5' is dummuy value not used
    
    0 6 3 # 3rd column '3' is dummuy value not used
    1 6 4 # 3rd column '4' is dummuy value not used
    3 6 5 # 3rd column '5' is dummuy value not used
    6 6 6 # 3rd column '6' is dummuy value not used
    e
    
    pause -1
    

    However, as far as the plottings of this type of figure are concerned, I am dissatisfied with the following points.
    1) Splot (3d plotting command) is not intuitive for drawing 2D data.
    2) We need to use a parametric representation (u,v) to superimpose other 2D curves on top of the mesh diagram.
    3) We have to take into account dummy data to construct the grid (see inline data in the above script).
    4) We can not construct the desired grid with data containing NaN for pm3d
    In particular, I have been unable to solve the problem in 4) until now. The plot style "with image" can appropriately treat the NaN values, but it is the plot style only for equally spaced mesh data.

    If the plotting style 'with rectangles' in this feature request is implemented, I think it would be possible to plot "image-rectangles.png" with the following simple or 'straightforward' description.

    set palette maxcolors 5
    set style fill transparent solid 0.9 noborder
    set xrange [0:6]
    set yrange [0:6]
    set cbrange [0:5]
    set size ratio 1
    unset key
    
    #########################################################################
    # FYI: Plotting circles instead of meshes for the same inline data
    # plot "-" using (($1+$3)/2):(($2+$4)/2):(0.5):5 with circles lc palette, \
    #      4/x with lines lw 3 lc rgb 'black'
    #
    
    plot "-" using 1:2:3:4:5 with rectangles lc var, \
         4/x with lines lw 3 lc rgb 'black'
    0 0 1 1 0    # rect: (0,0)-(1,1) value: 0
    1 0 3 1 1    # rect: (1,0)-(2,1) value: 1
    3 0 6 1 2    # rect: (3,0)-(6,1) value: 2
    0 1 1 3 1    # rect: (0,1)-(1,3) value: 1
    1 1 3 3 NaN    # rect: (1,1)-(3,3) value: 2
    3 1 6 3 3    # rect: (3,1)-(6,3) value: 3
    0 3 1 6 2    # rect: (0,3)-(1,6) value: 2
    1 3 3 6 3    # rect: (1,3)-(3,6) value: 3
    3 3 6 6 4    # rect: (3,3)-(6,6) value: 4
    e
    
    pause -1
    

    As for the name of the new plot style, I'm concerned about whether 'with rectangles' is appropriate.

    Thank you for reading the long explanation.

     
  • Ethan Merritt

    Ethan Merritt - 2020-08-09

    Your proposed input format for 'with rectangles' would be more compact than the input format for 'with polygons'. This is an advantage when the user is creating input files by hand. However from the perspective of a program or script that is creating a data file this is a small difference. In either case the description of each rectangle can be written by a single write or printf statement.

    Without making any decision at this time about whether it is worth implementing your proposed new style in a future gnuplot version, let me show how your plot can be created using commands accepted already by gnuplot.

    This example uses 2D plot style "with filledcurves". I have attached the complete command file, but the command itself is this:

    plot $MESH using 1:2:3 with filledcurves closed fc palette, 4/x with lines lw 3 lc 'black'
    

    It was intended that the new plot style "with polygons" would work exactly this way in a 2D plot. The user manual says 'plot with polygons' is currently treated as 'plot with filledcurves closed'. Unfortunately, this example shows that this statement is not correct, since the example fails if the plot command uses with polygons instead. That is a bug and should be fixed. For now, however, you can use the filledcurves style with existing gnuplot versions 5.0, 5.2, 5.4.

     

    Last edit: Ethan Merritt 2020-08-09
  • Hiroki Motoyoshi

    Thank you for providing an example of using 'with polygons' (currently 'with filledcurves' until bug-fix). And I'm sorry I didn't discover this method on my own with your intentions fully in mind.

    First of all, I'm pleased with the example you've given because many of the problems I've been having trouble with can be solved this way.
    I did not expect 'with filledcurves closed' to have such a use.

    Secondly, I also found a way to create the same diagram with another existing plotting style 'with boxxyerror'.

    gnuplot> ? boxxyerror
    The boxxyerror plot style is only relevant to 2D data plotting.
    It is similar to the xyerrorbars style except that it draws rectangular areas
    rather than crosses. It uses either 4 or 6 basic columns of input data.
    4 columns: x y xdelta ydelta
    6 columns: x y xlow xhigh ylow yhigh
    :
    An additional (5th or 7th) input column may be used to provide variable
    (per-datapoint) color information (see linecolor and rgbcolor variable).

    I used the 7-columns style and changed the order of the data by 'using' (see attachments). I recognized that the plotting style I proposed was mostly the same as "boxxyerror".

    Now, on my "Feature Request", I'd like to focus on whether it's possible to provide a part of the functionality of 'with boxxyerror' as 'with rectangles'. The above proposal is inspired by the fact that the functionality of 'with filledcurves closed' is provided as 'with polygons'. Additionally, I wonder if it would be possible to implement it so that the sequence of data would be "x1 y1 x2 y2 value" by association with the 'set object rectangle from ... to ...'.

    I've been struggling to plot the example diagram, but I couldn't have found it without your help in the last few days of correspondence. I think ordinary users like me would be very grateful for an alias to help them find a suitable plotting style for the drawing they want to do like 'polygons' <-> 'filledcurves'.

    Of course, we will use "boxxyerrors" until this feature request is accepted :-)

    Thanks.

     
  • Ethan Merritt

    Ethan Merritt - 2020-08-11

    You are correct that "boxxyerrors" is even closer to the proposed "rectangles" style in that each rectangle is completely described by a single line of input data. A simple implementation of "with rectangles" as described above could rearrange the four coordinates x0,y0,x1,y1 into the six coordinates x0,y0,x0,x1,y0,y1 and hand them off to the existing code for style "boxxyerrors".

    Another example that exists already in gnuplot is the recently added style "with arrows" that provides the same output as "with vectors" but uses a different set of input parameters to describe it.

     
  • Hiroki Motoyoshi

    Gnuplot has objects of label, arrow, rectangle, circle, polygon for annotation. And the part of them (label, arrow, circle polygon) were also implemented as the primitives of plotting styles. I think the rectangle object is also eligible to be implemented as a plotting style primitive.

    I have recognized that "boxxyerror" style is mostly the same as the style I propose here. But, I think the name and the order of the data is important. Indeed, 'boxxyerror' is a suitable style name for the purpose of showing the error range of the data (I noticed that it works with abbreviation "boxxy" !).

    However, I think 6-columns style of 'boxxyerror' is redundunt for the more general purpose of drawing rectangles.

    6 columns: x y xlow xhigh ylow yhigh

    The first 'x' and 'y' do not make sense to specify the shape of the rectangle,
    even for 'boxxyerror' style.

    Here, I can now add the applications using 'with rectangles',

    • Plotting 2D unequal mesh data
    • Plotting sparse mesh on a 2D grid
    • Display the ROI of object recognition
    • heatmaps with unequal mesh
    • vertically stacked bar chart (like polulation pyramid)
    • Guntt chart in demo page with rectangle instead of vector.

    Thanks.

     
  • Ethan Merritt

    Ethan Merritt - 2020-08-12

    If the primary motivation is to display a mesh, as opposed to individual arbitrarily placed rectangles, then it is also worth considering a different approach. Gnuplot supports a data input format "nonuniform matrix" that exists exactly for the purpose of describing a 2D mesh with unequal spacing. It would seem logical that just as a heatmap can be drawn for a uniform matrix with the command

     plot 'data' matrix with image
    

    There could be some variant of the command or some equivalent command that produces a heatmap from a nonuniform matrix. I have not really thought this through but perhaps it would be sufficient to allow the command

    plot 'data' matrix nonuniform with image
    

    Note that it internally this would not really be "with image" because the image code only works for uniform grids, but that distinction could be hidden from the user. I can see some problems, however, starting with the issue of what exactly each (x,y) coordinate means when creating the plot. Are these the center of the pixel? the lower left corner? Currently the code would most naturally treat it as the center of the pixel, but I would guess that for most data sets the "z value" represents a measurement exactly at one of the specified (x,y) coordinates, not at the midpoint between successive coordinates.

     
  • Hiroki Motoyoshi

    Thank you for the thought of the 'nonuniform matrix' input.

    If the primary motivation is to display a mesh, as opposed to individual arbitrarily placed rectangles, then it is also worth considering a different approach. Gnuplot supports a data input format "nonuniform matrix" that exists exactly for the purpose of describing a 2D mesh with unequal spacing.

    The manual says,

    gnuplot> ? matrix nonuniform
    :

    Thus the data organization for non-uniform matrix input is

      <N+1>  <x0>   <x1>   <x2>  ...  <xN>
       <y0> <z0,0> <z0,1> <z0,2> ... <z0,N>
       <y1> <z1,0> <z1,1> <z1,2> ... <z1,N>
        :      :      :      :   ...    :
    

    which is then converted into triplets:

      <x0> <y0> <z0,0>
      <x0> <y1> <z0,1>
      <x0> <y2> <z0,2>
       :    :     :
      <x0> <yN> <z0,N>
      <x1> <y0> <z1,0>
      <x1> <y1> <z1,1>
       :    :     :
    

    This format is essentially the same as the pm3d's 3-columns input.

    If we represent an nonuniform mesh with precisely the right number of data points, it will look like this.

      <N+1>  <x0>       <x1>       <x2>   ...  <xN-1>        <xN>
       <y0>   +----------+----------+           +------------+
              |  <z0,0>  |  <z0,1>  |    ...    |  <z0,N-1>  |
       <y1>   +----------+----------+           +------------+
              |  <z1,0>  |  <z1,1>  |    ...    |  <z1,N-1>  |
       <y2>   +----------+----------+           +------------+
         :    |      :   |     :    |     :     |     :      |
     <yN-1>   +----------+----------+           +------------+
              | <zN-1,0> | <zN-1,1> |    ...    | <zN-1,N-1> |
       <yN>   +----------+----------+           +------------+
    

    The position of '+' mark is the grid point and z in mesh is the mesh value. The x- and y-axis should have (N+1) elements, z values should have (N, N) elements.

    If we were to use the "nonuniform matrix" format as is, then we would have to treat the N columns and N rows of z as dummy data. It means that the mesh value would be represented by the index of the lower-left corner point of the mesh. The inclusion of the dummy data is the same situation as in the "with steps" for 1D data. Or it could be the same as the pm3d option 'set pm3d map corners2color c1'.

    If the support on nonuniform mesh with the 'image'-like style, it is excellent. The data is compact, and the "with image" already handles NaN correctly. Additionally, in the plot of such a dense mesh, the "with rectangles" style has the problem of drawing the mesh border twice.

    You seem to think that there are two approaches to this "feature request".

    (1) the plotting style like "image" which accepts "nonuniform matrix" data for a dense mesh.
    (2) the plotting style "rectangles" for a sparse mesh

    Although I think that both of these styles, if implemented, are broadly applicable plotting styles, the primary motivation is to plotting a dense mesh (approach 1) as you said.

     
    • Ethan Merritt

      Ethan Merritt - 2020-08-13

      As you say, this would be the same as using with pm3d. The only difference would be that it can be issued as a 2D plot command rather than a 3D projection set view map; splot.... If the projection is acceptable, then here are the commands:

      $DATA << EOD
      xx 0 1 3 6
      0  0 1 2 6
      1  1 NaN 3 6
      3  2 3 4 6
      6  6 6 6 6
      EOD
      
      
      set view map
      set size ratio 1
      set xrange [ 0.00000 : 6.00000 ]
      set yrange [ 0.00000 : 6.00000 ]
      set cbrange [ 0.00000 : 5.00000 ]
      set rrange [ * : * ] noreverse writeback
      set pm3d noborder corners2color c1
      set palette maxcolors 5
      unset key
      
      splot $DATA nonuniform matrix using 1:2:(0):3 with pm3d, \
            '+' using 1:(4./$1):(0) with lines lw 3 lc rgb "black"
      
       
  • Hiroki Motoyoshi

    Your example with pm3d works correctly!

    So far, I've come across three examples of the plot I didn't think could be drawn in gnuplot.

    (1) "plot ... with polygons" (plot)
    (2) "plot ... with boxxyerrors" (plot)
    (3) "splot ... with pm3d" with "set pm3d map ..."

    It's a little different from what I suggested, but I'm pleased that I was able to know how to draw the figure. I still have kind of wish the plotting style discussed here could be introduced in a future version, but I'll make sure that I can use these three methods well for my problems.

    Thank you for spending your valuable time with my request.

     
  • Hiroki Motoyoshi

    Tested usability with real data

    (1) "plot ... with polygons"
    (2) "plot ... with boxxyerrors"
    (3) "splot ... with pm3d" with "set pm3d map ..."

    The method in (1) was useful enough. Although the format of the data is more cumbersome than that of (2), it is very interesting that it can be used for non-orthogonal grids as well. However, I couldn't draw the figure properly if the following three conditions are met.
    a) Binary data input
    b) Include NaN in the color specification
    c) "set datafile missing NaN" is set
    Therefore, it is necessary that (c) is not used at the same time as the plotting of the required data. If data is given in ascii format, this probrem doesn't occur.

    The method (2) is the most convenient for me when I tried it on my real data. The data are compact and easy to use.

    The method of (3) was also generally available. Using multiplot and setting appropreate margins, the 2D projection of pm3d can be overlaid with other 2D plots. However, when the range of x- or y-axes is out of the grid, the edge color area is not clipped and goes out of the graph (attachment).

    I select the following two 'plotting styles to draw rectangle meshes".

    (1) "plot ... with polygons"
    (2) "plot ... with boxxyerrors"

    Thanks.

     
  • Ethan Merritt

    Ethan Merritt - 2022-01-15
    • status: open --> closed-out-of-date
    • Group: -->
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.