[ncl-talk] Drawing several polygons on a xy plot

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Wed Mar 10 05:54:29 MST 2021


Gsn_add_polygon does not work like that.  It draws only a single polygon
per call, and you must specify all vertices of the polygon.  Missing values
can not be used to create discontinuities or unfilled areas of a polygon.

The simplest approach is to draw multiple polygons, one narrow rectangle
for each year on the X axis.  Loop over years, and skip (i.e. do not draw)
polygons unless the IPO index is negative.  Within the loop, remember to
assign each polygon to a different element of a type graphics array, to
satisfy the memory requirement of the function.

    poly = new (nyear, graphic)
    do i = 0, nyear-1
        if (temp_amip2_tpi_year_filter(i) .lt. 0) then
            poly(i) = gsn_add_polygon (...)
        end if
    end do

I leave it up to you to calculate the correct polygon vertices for each
year.  Your initial code version is almost doing this already.  I recommend
using a fully closed polygon for each year.  That is 5 vertices, not 4, for
each rectangle.  Function behavior for non-closed polygons is undefined.


On Wed, Mar 10, 2021 at 3:20 AM Giorgio Graffino via ncl-talk <
ncl-talk at mailman.ucar.edu> wrote:

> Short addition to this thread: I'm running NCL 6.6.2 on Linux
> 5.4.101-1-MANJARO.
>
> Best,
> Giorgio
>
> ----Messaggio originale----
> Da: ncl-talk at mailman.ucar.edu
> Data: 10-mar-2021 0.11
> A: <ncl-talk at ucar.edu>
> Ogg: [ncl-talk] Drawing several polygons on a xy plot
>
> Dear NCL people,
>
> I'm trying to draw several polygons on a time series plot, to obtain
> something like this (
> https://www.nature.com/articles/nclimate2106/figures/1). Basically, I
> need separate filled boxes to be drawn anywhere the IPO index is negative.
> I'd like to not draw individual polygons myself, because I should set the x
> and y ranges explicitly. (That is, at all times when the IPO index changes
> sign). Rather, I'd like the boxes to be drawn automatically, without
> knowing where the index is negative.
>
> So far, my best attempt at the problem is to create a 1-D mask, setting
> all positive value of the index to missing values. Then, I'm creating one
> big box over the whole domain, relying on the missing values of the masked
> time series. The relevant snippet of my code is shown here.
>
> ;; Create polygons where AMIP II TPI/IPO index is negative
>  temp_amip2_tpi_year_filter_neg =
> where(temp_amip2_tpi_year_filter.lt.0.,temp_amip2_tpi_year_filter,temp_amip2_tpi_year_filter@
> _FillValue)
>  temp_amip2_tpi_year_filter_neg_xp =
> new((/2*nyear/),typeof(temp_amip2_tpi_year_filter_neg),temp_amip2_tpi_year_filter_neg@
> _FillValue)
>  temp_amip2_tpi_year_filter_neg_yp =
> new((/2*nyear/),typeof(temp_amip2_tpi_year_filter_neg),temp_amip2_tpi_year_filter_neg@
> _FillValue)
> do i = 0, nyear-1
>  temp_amip2_tpi_year_filter_neg_yp(i)          =
> temp_amip2_tpi_year_filter_neg(i)+1.e2
>  temp_amip2_tpi_year_filter_neg_xp(i)          = years(i)
>  temp_amip2_tpi_year_filter_neg_xp(2*nyear-1-i) = years(i)
>  temp_amip2_tpi_year_filter_neg_yp(2*nyear-1-i) =
> temp_amip2_tpi_year_filter_neg(i)-1.e2
> end do
>
> After, I'm calling gsn_add_polygon to draw the boxes over the time series
> plot, but I'm getting only one big box (see attached). It seems that the
> function doesn't care about missing values, and in the description that's
> clearly stated ("If a missing value is encountered in *x* and/or *y*,
> then this pair is ignored, but the polygon will still be closed";
> https://www.ncl.ucar.edu/Document/Graphics/Interfaces/gsn_add_polygon.shtml).
>
>
> My question: is there a way to make gsn_add_polygon not closing the
> polygon? Is there a better way to draw those polygons with NCL?
>
> Best,
> Giorgio
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20210310/35affa78/attachment.html>


More information about the ncl-talk mailing list