简体   繁体   中英

How do I find information about a function in python?

I know in R you can just type ?"function_name". How do you do this in python? Specifically, I am trying to find information about set_position in the pyplot library.

help(function)

should do the trick.

Demo:

def func():
    """
    I am a function who doesn't do anything,
    I just sit in your namespace and crowd it up.
    If you call me expecting anything
    I'll just return to you the singleton None
    """
    pass

help(func)

Try running in ipython , in which case you can type:

In [1]: from matplotlib import pyplot as pl

In [2]: pl.set_position?
Object `pl.set_position` not found.

Here, you have to use google to find out that set_position is a method of the Axes class:

In [3]: pl.Axes.set_position?
Type:       instancemethod
String Form:<unbound method Axes.set_position>
File:       /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py
Definition: pl.Axes.set_position(self, pos, which='both')
Docstring:
Set the axes position with::

  pos = [left, bottom, width, height]

in relative 0,1 coords, or *pos* can be a
:class:`~matplotlib.transforms.Bbox`

There are two position variables: one which is ultimately
used, but which may be modified by :meth:`apply_aspect`, and a
second which is the starting point for :meth:`apply_aspect`.


Optional keyword arguments:
  *which*

    ==========   ====================
    value        description
    ==========   ====================
    'active'     to change the first
    'original'   to change the second
    'both'       to change both
    ==========   ====================

In ipython its supereasy -- just append ? (or ?? for extended info with source code) to the function in question.

I always use it when working interactively in matplotlib :

In [2]: from matplotlib.axes import Axes

In [3]: Axes.set_position??
Type:       instancemethod
String Form:<unbound method Axes.set_position>
File:       /home/tzelleke/.local/modules/active_python_2.7/lib/python2.7/site-packages/matplotlib/axes.py
Definition: Axes.set_position(self, pos, which='both')
Source:
    def set_position(self, pos, which='both'):
        """
        Set the axes position with::

          pos = [left, bottom, width, height]

        in relative 0,1 coords, or *pos* can be a
        :class:`~matplotlib.transforms.Bbox`

        There are two position variables: one which is ultimately
        used, but which may be modified by :meth:`apply_aspect`, and a
        second which is the starting point for :meth:`apply_aspect`.


        Optional keyword arguments:
          *which*

            ==========   ====================
            value        description
            ==========   ====================
            'active'     to change the first
            'original'   to change the second
            'both'       to change both
            ==========   ====================

        """
        if not isinstance(pos, mtransforms.BboxBase):
            pos = mtransforms.Bbox.from_bounds(*pos)
        if which in ('both', 'active'):
            self._position.set(pos)
        if which in ('both', 'original'):
            self._originalPosition.set(pos)

In [4]: 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM