0

I have surfed online but still not sure when we do

import matplotlib.pyplot

matplotlib is a file ? and pyplot is a class ? Could some one explain how they are structured.

Also all libraries that we get, can we see code for them .In Pycharm I just need to press ctrl+left click. But will it work for lets say a base function like casting operator - int().

Also for above example for file matplotlib how can I see all the possible functions and classes in them , is there any such function ?

1
  • 2
    "like casting operator - int()." int is not an operator. It is a type, a constructor of int objects. Just like if I wrote class Integer: ... then used it like Integer(something) Commented Nov 29, 2019 at 3:55

3 Answers 3

3

matplotlib is a package, when you you import it, to Python it looks like any module.

pyplot is a module as well, but it's a module inside the matplotlib package.

You can ask Python itself:

>>> from matplotlib import pyplot
>>> type(pyplot)
<class 'module'>
>>> import matplotlib
>>> type(matplotlib)
<class 'module'>

A class would just be a type:

>>> from matplotlib import Parameter
>>> type(Parameter)
<class 'type'>

And to see what a module (or a class) has to offer, use dir():

>>> dir(matplotlib)
['LooseVersion', 'MatplotlibDeprecationWarning', 'MutableMapping', 'Parameter', 'Path', 'RcParams', 'URL_REGEX', '_DATA_DOC_APPENDIX', '_DATA_DOC_TITLE', '_ExecInfo', '__bibtex__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_add_data_doc', '_all_deprecated', '_check_versions', '_cm', '_cm_listed', '_color_data', '_constrained_layout', '_create_tmp_config_or_cache_dir', '_deprecated_ignore_map', '_deprecated_map', '_deprecated_remain_as_none', '_ensure_handler', '_error_details_fmt', '_get_config_or_cache_dir', '_get_data_path', '_get_executable_info', '_get_xdg_cache_dir', '_get_xdg_config_dir', '_image', '_init_tests', '_label_from_arg', '_layoutbox', '_log', '_logged_cached', '_mathtext_data', '_open_file_or_url', '_path', '_preprocess_data', '_pylab_helpers', '_rc_params_in_file', '_replacer', '_version', 'afm', 'artist', 'atexit', 'axes', 'axis', 'backend_bases', 'backend_tools', 'backends', 'bezier', 'blocking_input', 'category', 'cbook', 'checkdep_dvipng', 'checkdep_ghostscript', 'checkdep_inkscape', 'checkdep_pdftops', 'checkdep_ps_distiller', 'checkdep_usetex', 'cm', 'collections', 'colorbar', 'colors', 'compare_versions', 'container', 'contextlib', 'contour', 'cycler', 'dates', 'dedent', 'defaultParams', 'default_test_modules', 'docstring', 'dviread', 'figure', 'font_manager', 'fontconfig_pattern', 'ft2font', 'functools', 'get_backend', 'get_cachedir', 'get_configdir', 'get_data_path', 'get_home', 'get_label', 'get_py2exe_datafiles', 'gridspec', 'image', 'importlib', 'inspect', 'interactive', 'is_interactive', 'is_url', 'legend', 'legend_handler', 'lines', 'locale', 'logging', 'markers', 'mathtext', 'matplotlib_fname', 'mlab', 'mplDeprecation', 'namedtuple', 'numpy', 'offsetbox', 'os', 'patches', 'path', 'pprint', 'projections', 'pyplot', 'quiver', 'rc', 'rcParams', 'rcParamsDefault', 'rcParamsOrig', 'rc_context', 'rc_file', 'rc_file_defaults', 'rc_params', 'rc_params_from_file', 'rcdefaults', 'rcsetup', 're', 'sanitize_sequence', 'scale', 'set_loglevel', 'shutil', 'spines', 'stackplot', 'streamplot', 'style', 'subprocess', 'sys', 'table', 'tempfile', 'test', 'texmanager', 'text', 'textpath', 'ticker', 'tight_bbox', 'tight_layout', 'tk_window_focus', 'transforms', 'tri', 'units', 'use', 'validate_backend', 'widgets']
Sign up to request clarification or add additional context in comments.

2 Comments

A package is a module; it's just a module that can contain other modules.
A package is really just a collection of modules though - from the perspective of using it in Python, it's no different from a module, but when you're creating one or the other, there's difference. docs.python.org/3/tutorial/modules.html
1
import matplotlib

means that matplotlib is a "module", which is a namespace. it also means that matplotlib.pyplot is a module, because that's what the import statement does. It loads modules.

So pyplot is not a class. And Python classes should start with capital letters, too.

If this import succeeds, it means that "magically", python at runtime knows what matplotlib is, because python has a path of places in your filesystem to look. The list of places is determined when Python is installed.

If you think of it as matplotlib.py, you'll conceptually be correct. When you import a module, python will look for a file with that name in its path. As with most library packaging systems, the import statement could be abstracting some actual complexity. Big libraries can be split over multiple files and so on.

The module pyplot can only be found by first accessing matplotlib. This could be mapped to a subdirectory in the file system. Directories which contain sub-modules are marked via an __init__.py file and if you search to learn more about __init__.py files, you will learn about the topic of python modules.

The import statement you gave as an example lets you refer to matplotlib.pyplot to access actually useful things, such as functions and classes.

So if there was a function "show()" you could refer to

matplotlib.pyplot.show() 

in your code

def show_chart():
    matplotlib.pyplot.show() #made-up example

if you had instead done

from matplotlib import pyplot

you would refer to

pyplot.show()

which saves some typing.

if pyplot has a class "Chart", you could do

my_chart = pyplot.Chart()

PS there is a version of the python console called ipython If it is installed on your system, Pycharm will use it when you do Tools -> Python Console.

Try

>>> import datetime
>>> ??datetime

the ?? query tells you a lot about the module, although it actually just accesses documentation that you can also access in other ways, as you have discovered If that example doesn't work, ipython is not installed, so google for that. It has some very nice shortcuts.

Comments

0

You can get the detailed structure of the matplotlib project at https://github.com/matplotlib/matplotlib/tree/master/lib/matplotlib

Make sure that your IDE has path set to where you have installed the libraries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.