# Re: Size of viewport

From: Ivan Lima <ivan_at_nyahnyahspammersnyahnyah>
Date: Fri Feb 26 2010 - 07:02:27 MST

Thank you, Mary.

```--
Ivan Lima
Woods Hole Oceanographic Institution, MC&G MS #25
360 Woods Hole Road, Woods Hole, MA 02543-1543 USA
On Thu, Feb 25, 2010, Mary Haley wrote:
> Ivan,
>
> Here's a sample code and the resultant output from maximizing an area
> with random plots draw on it. These plots are not truly random,
> because they are all the same size.  :-)
>
> However, you should be able to adapt this code for your own use.
>
> I created a function "set_device_coords" that sets the PS/PDF device
> coordinates, based on the area of the unit square you want to
> maximize. Unfortunately, this means you need to know the min/max of
> the unit square you're drawing to (there's no way to currently
> calculate it).
>
> I need to implement a "get_bounding_box" type of function that allows
> you to get the bounding box that encloses a particular plot. This
> way, you can easily get the min/max of the area you're drawing to.
>
> For now, I used the ndc_draw_grid procedure to eyeball the area I
> wanted to maximize.
>
> You need to call set_device_coords before you start drawing to a
> frame, because you'll get unexpected results otherwise.
>
>
> --Mary
> import numpy, Ngl
>
>
> #
> # Function to set the PS/PDF device coordinates so that
> # your PyNGL plots are maximized on the given paper size.
> #
> # wks - the workstation opened with Ngl.open_wks()
> #
> # top, bot, lft, rgt - represent a unit square. These
> #     should be set to the min/max of the actual area
> #     that your plots reside in.
> #
> def set_device_coords(wks,top=1.0,bot=0.0,lft=0.0,rgt=1.0,\
>                       paper_orient=None,paper_width=8.5, \
>                       paper_height=11.0,paper_margin=0.5):
> #
> # Initialization
> #
>   dpi        = 72                       # Dots per inch
>   dpi_pw     = paper_width  * dpi;
>   dpi_ph     = paper_height * dpi;
>   dpi_margin = paper_margin * dpi;
>
> #
> # Get paper height/width in dpi units
> #
>   pw = rgt - lft;
>   ph = top - bot;
>
>   lx = dpi_margin;
>   ly = dpi_margin;
>
>   ux = dpi_pw - dpi_margin;
>   uy = dpi_ph - dpi_margin;
>
>   dw = ux - lx;
>   dh = uy - ly;
>
>   if (paper_orient == "portrait") or \
>      (paper_orient == None and (ph/pw) >= 1.):
>     paper_orient = "portrait"
>
>     if ( (ph/pw) > (dh/dw) ):
>                                       # paper height limits size
>       ndc2du = dh / ph
>     else:
>       ndc2du = dw / pw
>
> #
> # Compute device coordinates.
> #
>     lx = dpi_margin + 0.5 * ( dw - pw * ndc2du) - lft * ndc2du;
>     ly = dpi_margin + 0.5 * ( dh - ph * ndc2du) - bot * ndc2du;
>     ux = lx + ndc2du;
>     uy = ly + ndc2du;
>   else:
> #
> # If area or plot is wider than it is high, then default to
> # landscape if orientation is not specified.
> #
>     paper_orient = "landscape"
>
>     if ( (pw/ph) > (dh/dw) ):
>                                       # paper height limits size
>       ndc2du = dh / pw
>     else:
>       ndc2du = dw / ph
>
> #
> # Compute device coordinates.
> #
>     ly = dpi_margin + 0.5 * (dh - pw * ndc2du) - (1.0 - rgt) * ndc2du;
>     lx = dpi_margin + 0.5 * (dw - ph * ndc2du) - bot * ndc2du;
>     ux = lx + ndc2du;
>     uy = ly + ndc2du;
>
> #
> # Reset the PS/PDF device coordinates and the orientation.
> #
>   rlist                       = Ngl.Resources()
>   rlist.wkDeviceLowerX        = lx
>   rlist.wkDeviceLowerY        = ly
>   rlist.wkDeviceUpperX        = ux
>   rlist.wkDeviceUpperY        = uy
>   rlist.wkOrientation         = paper_orient
>
>   Ngl.set_values(wks,rlist)
>
>
> #
> # MAIN CODE
> #
>
>
> #
> #  Define coordinate data for dummy XY plots.
> #
> x = [10., 20.00, 30., 40.0, 50.000, 60.00, 70., 80.00, 90.000]
> y = [ 0.,  0.71,  1.,  0.7,  0.002, -0.71, -1., -0.71, -0.003]
>
> wks_type = "pdf"
> wks = Ngl.open_wks(wks_type,"xy3")
>
> res             = Ngl.Resources()
> res.nglFrame    = False
> res.nglDraw     = False
> res.nglMaximize = False
>
> res.vpWidthF  = 0.3
> res.vpHeightF = 0.45
>
> res.vpXF = 0.15
> res.vpYF = 0.97
> plot1 = Ngl.xy(wks,x,y,res)
>
> res.vpXF = 0.55
> res.vpYF = 0.97
> plot2 = Ngl.xy(wks,x,y,res)
>
> res.vpXF = 0.15
> res.vpYF = 0.48
> plot3 = Ngl.xy(wks,x,y,res)
>
> res.vpXF = 0.55
> res.vpYF = 0.48
> plot4 = Ngl.xy(wks,x,y,res)
>
> #
> # You must call set_device_coords before you start drawing
> # anything to the frame, or you may get unexpected results.
> #
> # If you know the limits before you create the plots, then
> # you can move this code right after the Ngl.open_wks call.
> #
> # It would be better if we could calculate the min/max of
> # the area we're drawing in. I need to implement the
> # NCL equivalent of "NhlGetBB" for PyNGL (retrieves the bounding
> # box of a plot).
> #
> if wks_type == "pdf" or wks_type == "ps":
>   set_device_coords(wks,top=0.98,bot=0.01,lft=0.11,rgt=0.85)
>
> # Draw a debug unit square grid to help determine box limits.
> # Ngl.draw_ndc_grid(wks)
>
> Ngl.draw(plot1)
> Ngl.draw(plot2)
> Ngl.draw(plot3)
> Ngl.draw(plot4)
>
> Ngl.frame(wks)
>
> Ngl.end()
>
>
> On Feb 25, 2010, at 11:11 AM, Ivan Lima wrote:
>
> >  Hi Mary,
> >
> >  I'm drawing multiple plots on one page. The individual plots have
> >  different sizes and shapes so I'm not using Ngl.panel. I use
> >  Ngl.panel all the time for multiple plots of the same size and
> >  shape.
> >
> >  OK, how do I maximize the plots in a non-square area? Or how can I
> >  make the "square" use the full height of the page?
> >
> >  Just one more clarification. So nglPaperMargin, nglPaperWidth,
> >  nglPaperHeight only work when nglMaximize = True ?
> >
> >  Thanks,
> >
> >  - Ivan
> >
> >-- Ivan Lima
> >Woods Hole Oceanographic Institution, MC&G MS #25
> >360 Woods Hole Road, Woods Hole, MA 02543-1543 USA
> >
> >
> >On Thu, Feb 25, 2010, Mary Haley wrote:
> >
> >>Hi Ivan,
> >>
> >>You said "multi-panel" plot, but then you said "the plot would fit
> >>nicely
> >>in letter sized paper".  Are you drawing multiple plots on one
> >>page or
> >>a single plot?
> >>
> >>If you are drawing multiple plots that are the same size, have you
> >>tried using Ngl.panel instead of using the vpXXXX resources?
> >>This will take care of resizing the plots so that they fit nicely on
> >>the page.
> >>
> >>If you are trying to position the plots yourself, then this is
> >>going to
> >>be done on an invisible unit square, and afterwards you will have
> >>to set some special PostScript resources to resize these plots so
> >>they
> >>are maximized in a non-square area (this is what "nglMaximize"
> >>does for
> >>you, but this only works for individual plots, or Ngl.panel).
> >>
> >>I can help you with the latter part, but first I want to see if
> >>Ngl.panel
> >>will work for you.
> >>
> >>--Mary
> >>
> >>On Feb 25, 2010, at 9:48 AM, Ivan Lima wrote:
> >>
> >>> I'm trying to create a multi-panel plot using vpXF, vpYF, vpWidthF
> >>> and vpHeightF. The plot is taller than it is wide and it would fit
> >>> nicely in letter sized paper. nglPaperOrientation is set to
> >>> 'portrait', but no matter what I do I can only plot inside a
> >>> relatively short rectangular area with big margins at the top and
> >>> bottom of the page. I tried changing nglPaperMargin, nglPaperWidth,
> >>> nglPaperHeight and wkPaperSize but they don't seem to have any
> >>> effect. How do I reduce the top and bottom margins in the plot? Or
> >>> for that matter, how do I control the size of the margins so I can
> >>> use more area in the page without "clipping" part of my plot?
> >>>
> >>> Thank you for your help,
> >>>
> >>>--
> >>>Ivan Lima
> >>>Woods Hole Oceanographic Institution, MC&G MS #25
> >>>360 Woods Hole Road, Woods Hole, MA 02543-1543 USA
> >>>
> >>>_______________________________________________
> >>>pyngl-talk mailing list
> >>>List instructions, subscriber options, unsubscribe:
> >>>http://mailman.ucar.edu/mailman/listinfo/pyngl-talk
> >>
> >
>
```
Received on Fri Feb 26 07:02:42 2010

This archive was generated by hypermail 2.1.8 : Mon Mar 01 2010 - 09:12:40 MST