Menu

#411 Variable fill colour for polygons when using "filledcurves closed"

None
closed-fixed
nobody
None
5
2016-01-28
2015-01-14
Tom Dwelly
No

I often use gnuplot to make figures illustrating the locations of various filled polygons of arbitrary shape. The x,y coordinates of the polygon vertices are stored in a single data file, with blank lines separating each polygon. I often need to plot many hundreds or thousands of polygons.

One can plot these polygons using the "with filledcurves closed" plotting style and with a fixed fill style. In many cases this is is more than enough and looks great. However, it would be very useful to also be able to control the fill style of each polygon individually, where the fill colour would be derived from e.g. the current palette. I have seen one work-around that achieves some of this functionality: (http://www.gnuplotting.org/us-states-and-election-results/), but this would become unworkable when more than a few different colours are required. It would be much nicer to have the functionality available directly from the plot line.

Just to be explicit, the fill style within each polygon should be constant, but would vary from polygon to polygon dependent on the value of the third plot argument.

I think that such a feature would be a very valuable enhancement to gnuplot's general plotting capabilities.

Thanks in advance

Discussion

  • Ethan Merritt

    Ethan Merritt - 2015-01-14

    I may not understand fully what you are doing, but so far as I can see gnuplot already does this.

    I take it that your plot command is something like this:

      plot 'data' using 1:2 with filledcurves closed
    

    where each successive block of lines in the data file gets a new color. By default these successive colors have nothing to do with the data values per se, although you are free to define a preferred sequence of colors if you like ("set linetype"). You are free to introduce a third using specifier that ties the color to something else. Suppose your curves are already sorted on Z, so that increasing position in the file (the "index") corresponds to higher z value. Then this command will do what you describe:

      plot 'data' using 1:2:(column(-2)) with filledcurves closed fillcolor palette z
    

    Column -2 is returns the index value of the current curve. See "help pseudocolumns"

     

    Last edit: Ethan Merritt 2015-01-14
  • Ethan Merritt

    Ethan Merritt - 2015-01-14

    [correction: I think "lc" for linecolor does work in the above command, but "fillcolor" makes it more obvious what's going on.]

     
  • Tom Dwelly

    Tom Dwelly - 2015-01-15

    Hi Ethan, thanks for the very fast response.

    I've tried both of your suggestions but unfortunately they result in essentially the same problem I was having before. Gnuplot seems to be picking up the variable colours when plotting the perimeter lines of the polygons, but is using a single fill colour for polygons. If this is not the expected behaviour then maybe this issue should be moved to the bug reports section.

    Here is a copy the gnuplot script file I'm using:

    reset
    infile = "test_polygons.txt"
    set terminal pdfcairo size 20.0cm,15.0cm font ",12"
    set out "test.pdf"
    set style fill transparent solid 0.6

    plot infile using 1:2 with filledcurves closed lw 2

    plot infile using 1:2:(column(-2)) with filledcurves closed lc palette z lw 2

    plot infile using 1:2:(column(-2)) with filledcurves closed fillcolor palette z lw 2

    plot infile using 1:2:(column(-2)) with filledcurves closed lc var lw 2

    set out

    I've put copies of the input data file and output plot here:
    http://www.mpe.mpg.de/~tdwelly/gnuplot/test_polygons.txt
    http://www.mpe.mpg.de/~tdwelly/gnuplot/test_polygons.pdf

    I'm seeing very similar outputs using the following gnuplot versions (on OSX):
    Version 4.6 patchlevel 6 last modified September 2014
    Version 5.0 patchlevel 0 last modified 2015-01-01
    Version 5.1 patchlevel 0 last modified 2014-09-03
    I've seen similar outputs when using the wxt, qt and png terminals and on a linux installation.

    Thanks again for your help.

     
  • Ethan Merritt

    Ethan Merritt - 2015-01-15

    You are right. The code is not as smart as it ought to be about recognizing that the variable color needs to be reevaluated whenever a new polygon is started. The fix should be trivial.

    But meanwhile, the following work-around should get you going.

    N = <some number of polygons>
    set cbrange [0:N]
    plot for [poly=0:N] infile index poly using 1:2 with filledcurves closed fillcolor palette cb poly lw 2
    

    It doesn't hurt to give a very large value of N, although you'll get warning messages if N is larger than the actual number of polygons in the file. If necessary, you could scan the file first:

    stats infile using 2
    N = STATS_blocks
    set cbrange [0:N]
    plot for [poly=0:N] infile index poly using 1:2 with filledcurves closed fillcolor palette cb poly lw 2
    
     

    Last edit: Ethan Merritt 2015-01-15
  • James Shaw

    James Shaw - 2015-02-09

    I'm trying to plot a heatmap with nonuniform areas. This last example works, but how can I adapt it to use palette values from the data, rather than colouring based on the index of the polygon? If I use palette z only the line strokes are coloured, not the fill.

     

    Last edit: James Shaw 2015-02-09
  • Ethan Merritt

    Ethan Merritt - 2016-01-28
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,4 +1,3 @@
    -
     I often use gnuplot to make figures illustrating the locations of various filled polygons of arbitrary shape. The x,y coordinates of the polygon vertices are stored in a single data file, with blank lines separating each polygon. I often need to plot many hundreds or thousands of polygons. 
    
     One can plot these polygons using the "with filledcurves closed" plotting style and with a fixed fill style. In many cases this is is more than enough and looks great. However, it would be very useful to also be able to control the fill style of each polygon individually, where the fill colour would be derived from e.g. the current palette. I have seen one work-around that achieves some of this functionality: (http://www.gnuplotting.org/us-states-and-election-results/), but this would become unworkable when more than a few different colours are required. It would be much nicer to have the functionality available directly from the plot line.
    
    • status: open --> closed-fixed
    • Group: -->
     

Log in to post a comment.