Skip to content

add_subplot with numpy.float64 argument gives error in twinx() #880

Closed

Description

@elinbloem

I was trying to get two scales, using the example http://matplotlib.sourceforge.net/examples/api/two_scales.html

If you change this:

ax1 = fig.add_subplot(111)

to this:

a = np.float64(1)
ax1 = fig.add_subplot(a,1,1)

I get the error copied below. The problem is that add_subplot() accepts a type that bubble() (called by twinx()) can't handle. Because the error occurs 10 lines later in the code it was particularly difficult to find.

I use the Enthought Python Distribution (free) 7.2-2 (32 bit), Matplotlib 1.1.0 using the WXAgg back end and Numpy 1.6.1 on OS X 10.7.4.

Thanks,

Robbert

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/IPython/utils/py3compat.py in execfile(fname, *where)
    173             else:
    174                 filename = fname
--> 175             __builtin__.execfile(filename, *where)

/xxx/test.py in <module>()
     15 
     16 
---> 17 ax2 = ax1.twinx()
     18 s2 = np.sin(2*np.pi*t)
     19 ax2.plot(t, s2, 'r.')

/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/axes.py in twinx(self)
   7366 
   7367         ax2 = self.figure.add_axes(self.get_position(True), sharex=self,
-> 7368             frameon=False)
   7369         ax2.yaxis.tick_right()
   7370         ax2.yaxis.set_label_position('right')

/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/figure.py in add_axes(self, *args, **kwargs)
    705 
    706         self._axstack.add(key, a)
--> 707         self.sca(a)
    708         return a
    709 

/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/figure.py in sca(self, a)
   1055     def sca(self, a):
   1056         'Set the current axes to be a and return a'
-> 1057         self._axstack.bubble(a)
   1058         for func in self._axobservers: func(self)
   1059         return a

/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/figure.py in bubble(self, a)
     77 
     78     def bubble(self, a):
---> 79         return Stack.bubble(self, self._entry_from_axes(a))
     80 
     81     def add(self, key, a):

/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/cbook.py in bubble(self, o)
   1201         """
   1202 
-> 1203         if o not in self._elements:
   1204             raise ValueError('Unknown element o')
   1205         old = self._elements[:]

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
---------------------------------------------------------------------------

Activity

  1. ghost assigned on Jun 13, 2012
  2. pelson commented on Aug 15, 2012

    @pelson
    Member

    Confirming.

    import matplotlib.pyplot as plt
    import numpy as np
    
    ax = plt.subplot(5.5, 1, 1)
    ax2 = ax.twinx()
    

    is fine, where

    import matplotlib.pyplot as plt
    import numpy as np
    
    ax = plt.subplot(np.float64(5.5), 1, 1)
    ax2 = ax.twinx()
    

    raises a ValueError

  3. added a commit that references this issue on Apr 12, 2013
  4. added a commit that references this issue on May 14, 2013
  5. dmcdougall commented on Jun 22, 2013

    @dmcdougall
    Member

    Someone reopen this if I closed this mistakenly. I believe it was resolved by #1092.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions