Matplotlib - Environment Setup



Matplotlib llibrary is highly compatible with various operating systems and Python environments. Setting up Matplotlib is relatively straightforward and its versatility makes it a valuable tool for visualizing data in Python. It involves ensuring that it is installed and configuring its behavior within our Python environment.

The below is the step-by-step guide to set the environment of the matplotlib library.

Installation

Matplotlib is often included in Python distributions like Anaconda. However if it's not installed we can do so using pip. The following is the command to install the matplotlib library.

pip install matplotlib

Checking the Installation

If we want to verify whether the installation is done or not then open a Python interpreter or a Jupyter Notebook and import Matplotlib library pyplot module by using the below code line.

import matplotlib.pyplot as plt

If no errors occur then the installation is successful otherwise there is a trouble in installation.

Backend Selection

Matplotlib has different "backends" responsible for rendering the plots. These backends can display figures in different environments e.g. in a Jupyter Notebook a separate window etc.

Interactive Backends (Great for Jupyter Notebook)

For enabling interactive plotting within Jupyter Notebook, we use the magic command %matplotlib. The below is the code line to be executed.

%matplotlib inline
# or
%matplotlib notebook

The %matplotlib inline command displays static images of our plot in the notebook while %matplotlib notebook allows interactive plots such as panning and zooming.

Non-Interactive Backend (when not using Jupyter)

When not working within a Jupyter environment then Matplotlib can use non-interactive backends. It automatically selects a suitable backend for our system. We can set a backend explicitly.

import matplotlib
matplotlib.use('Agg')  # Backend selection, 'Agg' for non-interactive backend

Configuration and Style

Matplotlib allows customization of default settings and styles. We can create a configuration file named `matplotlibrc` to customize the behavior.

Locating the Configuration File

To find where our Matplotlib configuration file is located we can run the below code using the Python editor.

import matplotlib
matplotlib.matplotlib_fname()

Creating/Editing Configuration

We can modify this file directly or create a new one to adjust various settings such as default figure size, line styles, fonts etc.

Testing the Setup

To ensure everything is set up correctly we can create a simple plot using Matplotlib.

Example
import matplotlib.pyplot as plt
x = [i2 for i in range(2,30)]
y = [i3 for i in range(2,30)]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Test Plot')
plt.show()

Running this code should display a simple plot with a line chart in the selected environment.

Output
Test Case

Incase Python 2.7 or 3.4 versions are not installed for all users, the Microsoft Visual C++ 2008 (64 bit or 32 bit forPython 2.7) or Microsoft Visual C++ 2010 (64 bit or 32 bit for Python 3.4) redistributable packages need to be installed.

If you are using Python 2.7 on a Mac, execute the following command −

xcode-select -install

Upon execution of the above command, the subprocess32 - a dependency, may be compiled.

On extremely old versions of Linux and Python 2.7, you may need to install the master version of subprocess32.

Matplotlib requires a large number of dependencies −

  • Python (>= 2.7 or >= 3.4)

  • NumPy

  • setuptools

  • dateutil

  • pyparsing

  • libpng

  • pytz

  • FreeType

  • cycler

  • six

Optionally, you can also install a number of packages to enable better user interface toolkits.

  • tk

  • PyQt4

  • PyQt5

  • pygtk

  • wxpython

  • pycairo

  • Tornado

For better support of animation output format and image file formats, LaTeX, etc., you can install the following −

  • _mpeg/avconv

  • ImageMagick

  • Pillow (>=2.0)

  • LaTeX and GhostScript (for rendering text with LaTeX).

  • LaTeX and GhostScript (for rendering text with LaTeX).

Advertisements