adding second formatter/locater set to axes objects

A question I have seen go by twice on SO is how to add a tick marks to
the top/right of a plot which are re-scaled version of the values on
the bottom/lefit axis (ex km/h on the left and mph on the right). My
understanding of the best way to do this is to use twin* and then keep
the range of the two axes in sync by hand.

I am proposing adding at set of alternate formatters/locators to the
`axis` objects which if they exist are used for the top/right labels.
Before I dive too deep into this, I want to check that a) there isn't
a better way to do this that I do not know and b) if this sounds like
a reasonable approach.

Tom

···

--
Thomas Caswell
tcaswell@...149...

For what it is worth, we attacked this problem about four years ago at my
work. We just implemented a simple formatter. A locator can also be done
in a similar manner if that is desired.

This is not exactly is a perfect solution, but it worked really well for us.

class ScaledFormatter( ticker.Formatter ):
   """: Takes an exisiting formatter and scales the value by a specified
amount.
   """
   def __init__( self, scale, formatter ):
      """Initialize the scaled formatter.

      = INPUT VARIABLES
      - scale The scale factor to apply to the values specified by the
                  other formatter.
      - formatter Another matplotlib formatter to use for formatting the
                  data labels.
      """
      self.scale = scale
      self.formatter = formatter

   def __call__( self, x, pos = None ):
      'Return the format for tick val x at position pos'
      # get the string form of the value
      valueStr = self.formatter( x, pos )

      if len(valueStr) == 0:
         return ''

      # remove the unicode hyphen
      valueStr = valueStr.replace( u'\u2212', '-' )

      # convert to a float
      value = float( valueStr )

      # now scale the value
      value *= self.scale

      # convert ot a Unicode string
      s = unicode( value )

      # add the unicode hyphen
      s.replace( '-', u'\u2212' )

      return s

   def set_locs( self, locs ):
      'Make sure the encompassed formatter get these set.'
      ticker.Formatter.set_locs( self, locs )
      self.formatter.set_locs( locs )

   def set_axis( self, axis ):
      'Make sure the encompassed formatter get these set.'
      ticker.Formatter.set_axis( self, axis )
      self.formatter.set_axis( axis )

   def set_view_interval( self, vmin, vmax ):
      'Make sure the encompassed formatter get these set.'
      ticker.Formatter.set_view_interval( self, vmin, vmax )
      self.formatter.set_view_interval( vmin, vmax )

   def set_data_interval( self, vmin, vmax ):
      'Make sure the encompassed formatter get these set.'
      ticker.Formatter.set_data_interval( self, vmin, vmax )
      self.formatter.set_data_interval( vmin, vmax )

   def set_bounds( self, vmin, vmax ):
      'Make sure the encompassed formatter get these set.'
      ticker.Formatter.set_bounds( self, vmin, vmax )
      self.formatter.set_bounds( vmin, vmax )

--James

From: Thomas Caswell [mailto:tcaswell@…149…]
Sent: Thursday, May 23, 2013 5:26 PM
To: matplotlib-devel@lists.sourceforge.net
Subject: [matplotlib-devel] adding second formatter/locater set to axes
objects

A question I have seen go by twice on SO is how to add a tick marks to the
top/right of a plot which are re-scaled version of the values on the
bottom/lefit axis (ex km/h on the left and mph on the right). My
understanding of the best way to do this is to use twin* and then keep the
range of the two axes in sync by hand.

I am proposing adding at set of alternate formatters/locators to the

`axis`

objects which if they exist are used for the top/right labels.
Before I dive too deep into this, I want to check that a) there isn't a

better

···

-----Original Message-----
way to do this that I do not know and b) if this sounds like a reasonable
approach.

Tom

--
Thomas Caswell
tcaswell@...149...

----------------------------------------------------------------------------
--

Try New Relic Now & We'll Send You this Cool Shirt New Relic is the only

SaaS-

based application performance monitoring service that delivers powerful

full

stack analytics. Optimize and monitor your browser, app, & servers with

just

a few lines of code. Try New Relic and get this awesome Nerd Life shirt!
http://p.sf.net/sfu/newrelic_d2d_may
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
matplotlib-devel List Signup and Options