NCAR / pyngl

Other
138 stars 30 forks source link

Add equivalent functionality to gsnLeftString and gsnRightString #11

Open jaredalee opened 5 years ago

jaredalee commented 5 years ago

Hi, I'd like to request that pyngl add functionality equivalent to the gsnLeftString and gsnRightString resources in NCL. I use those when plotting gridded model forecast data, using one to indicate the initialization time, and the other to indicate the valid time. I'm starting to play around with pyngl now, and that's one feature I've discovered that's missing from NCL that I use all the time.

Jared

KMFleischer commented 5 years ago

Hi Jared,

yes, these resources are still missing. But you can write a function to do it for you. I've attached a simple script which draws an xy-plot and adds annotations like gsnLeftString and gsnRightString at the top of the plot.

import Ngl

#-- Routine ngl_Strings: draw left, right and/or center string
def ngl_Strings(wks, plot, left='', center='', right=''):
    """
       ngl_Strings(wks, plot, left='', center='', right='')

       Add annotations
    - left, right or center string above plot

       Correspond to NCL's 
      gsnLeftString, gsnCenterString, gsnRightString'
    """
    assert str(getattr(wks,'__class__')  == "<class 'int'>"), 'ERROR - 1st parameter is not a Ngl wks'
    assert str(getattr(plot,'__class__') == "<class 'ngl.PlotIds'>"), 'ERROR - 2nd parameter is not a Ngl plot'

    vpx = Ngl.get_float(plot,"vpXF")         #-- retrieve value of res.vpXF from plot
    vpy = Ngl.get_float(plot,"vpYF")         #-- retrieve value of res.vpYF from plot
    vpw = Ngl.get_float(plot,"vpWidthF")     #-- retrieve value of res.vpWidthF from plot

    txres = Ngl.Resources()
    txres.txFontHeightF = 0.014              #-- font size for left, center and right string

    y = vpy + 0.025                          #-- y-position

    if(left != ''):
       txres.txJust = "CenterLeft"           #-- text justification
       x = vpx                               #-- x-position
       Ngl.text_ndc(wks, left, x, y, txres)  #-- add text to wks

    if(center != ''):
       txres.txJust = "CenterCenter"         #-- text justification
       x = vpx + vpw/2
       Ngl.text_ndc(wks, center, x, y, txres)#-- add text to wks

    if(right != ''):
       txres.txJust = "CenterRight"          #-- text justification
       x = vpx+vpw                           #-- x-position
       Ngl.text_ndc(wks, right, x, y, txres) #-- add text to wks

#-- main routine
def main():
    x = [1,2,3,4,5]
    y = [1,2,3,4,5]

    long_name = 'left string'
    units = 'right string'

    #-- generate an res object for workstation and open it
    wkres = Ngl.Resources()
    wks = Ngl.open_wks('png','plot_test_annotations',wkres)

    #-- set resources
    res = Ngl.Resources() 
    res.nglDraw     = False
    res.nglFrame    = False
    res.nglMaximize = False

    res.vpXF        = 0.15                  #-- viewport x-position
    res.vpYF        = 0.9                   #-- viewport y-position
    res.vpWidthF    = 0.7                   #-- viewport width
    res.vpHeightF   = 0.7                   #-- viewport height

    #-- create xy-plot
    plot = Ngl.xy(wks,x,y,res)

    #-- add left and right string at the top of the plot
    ngl_Strings(wks, plot, left=long_name, right=units)

    #-- draw all
    Ngl.draw(plot)
    Ngl.frame(wks)
    Ngl.end()

#-- call  main
if __name__ == '__main__':
        main()

-Karin

jaredalee commented 5 years ago

Hi Karin,

Thank you so much! This works great for me! My old way to get around this involved using Ngl.text_ndc to place the text strings, which required lots of trial and error for each new plot domain size that I was using, so that they would look good and be placed properly. This will simplify and standardize things greatly in my plots of WRF output.

Jared

On Tue, Oct 15, 2019 at 11:17 AM Karin Meier-Fleischer < notifications@github.com> wrote:

Hi Jared,

yes, these resources are still missing. But you can write a function to do it for you. I've attached a simple script which draws an xy-plot and adds annotations like gsnLeftString and gsnRightString at the top of the plot.

import Ngl

-- Routine ngl_Strings: draw left, right and/or center string

def ngl_Strings(wks, plot, left='', center='', right=''): """ ngl_Strings(wks, plot, left='', center='', right='')

   Add annotations
  • left, right or center string above plot

    Correspond to NCL's gsnLeftString, gsnCenterString, gsnRightString' """ assert str(getattr(wks,'class') == "<class 'int'>"), 'ERROR - 1st parameter is not a Ngl wks' assert str(getattr(plot,'class') == "<class 'ngl.PlotIds'>"), 'ERROR - 2nd parameter is not a Ngl plot'

    vpx = Ngl.get_float(plot,"vpXF") #-- retrieve value of res.vpXF from plot vpy = Ngl.get_float(plot,"vpYF") #-- retrieve value of res.vpYF from plot vpw = Ngl.get_float(plot,"vpWidthF") #-- retrieve value of res.vpWidthF from plot

    txres = Ngl.Resources() txres.txFontHeightF = 0.014 #-- font size for left, center and right string

    y = vpy + 0.025 #-- y-position

    if(left != ''): txres.txJust = "CenterLeft" #-- text justification x = vpx #-- x-position Ngl.text_ndc(wks, left, x, y, txres) #-- add text to wks

    if(center != ''): txres.txJust = "CenterCenter" #-- text justification x = vpx + vpw/2 Ngl.text_ndc(wks, center, x, y, txres)#-- add text to wks

    if(right != ''): txres.txJust = "CenterRight" #-- text justification x = vpx+vpw #-- x-position Ngl.text_ndc(wks, right, x, y, txres) #-- add text to wks

-- main routine

def main(): x = [1,2,3,4,5] y = [1,2,3,4,5]

long_name = 'left string'
units = 'right string'

#-- generate an res object for workstation and open it
wkres = Ngl.Resources()
wks = Ngl.open_wks('png','plot_test_annotations',wkres)

#-- set resources
res = Ngl.Resources()
res.nglDraw     = False
res.nglFrame    = False
res.nglMaximize = False

res.vpXF        = 0.15                  #-- viewport x-position
res.vpYF        = 0.9                   #-- viewport y-position
res.vpWidthF    = 0.7                   #-- viewport width
res.vpHeightF   = 0.7                   #-- viewport height

#-- create xy-plot
plot = Ngl.xy(wks,x,y,res)

#-- add left and right string at the top of the plot
ngl_Strings(wks, plot, left=long_name, right=units)

#-- draw all
Ngl.draw(plot)
Ngl.frame(wks)
Ngl.end()

-- call main

if name == 'main': main()

-Karin

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/NCAR/pyngl/issues/11?email_source=notifications&email_token=AKWMV74OPCDEJPY5LIJX23DQOX3JNA5CNFSM4HDXJKJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBJRPYQ#issuecomment-542316514, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKWMV74QSNGGWILJ4XXOTQTQOX3JNANCNFSM4HDXJKJQ .

--

My working day may not be your working day.Please do not feel obligated to reply to this email outside of your normal working day.

=============================== Jared A. Lee, Ph.D. Project Scientist II Research Applications Laboratory National Center for Atmospheric Research Boulder, Colorado, USA

Member, AMS Planning Commission

Email: jaredlee@ucar.edu (w) Phone: 303.497.8485 (w) Web: https://staff.ucar.edu/users/jaredlee