Python Matplot

Python Matplot

Python Matplot is a data visualization library built on NumPy arrays which support multiple OS Platforms. Its designed to work with the broader SciPy stack. The library was developed by John D. Hunter.

Mathplot borrows heavily from Mathlab type visualization and therefore it’s easier to move on from Mathlab and Octave to python for data scientists and engineers. It was adopted as the plotting package of choice of the Space Telescope Science Institute (the folks behind the Hubble Telescope), which financially supported Matplotlib’s development and greatly expanded its capabilities.

One of Matplotlib’s most important features is its ability to play well with many operating systems and graphics backends. Matplotlib supports dozens of backends and output types, which means you can count on it to work regardless of which operating system you are using or which output format you wish. This cross-platform, everything-to-everyone approach has been one of the great strengths of Matplotlib. It has led to a large user base, which in turn has led to an active developer base and Mat plotlib’s powerful tools and ubiquity within the scientific Python world.

For working on data science and machine learning, data has to be visualized to understand patterns and behavior of ML algorithms so that it can be fine-tuned for improving accurate predictions.

Note : Everything in mathplot is configurable and customizable, with a bit of tweak in code, you should be able to get desired visual results.

A Brief Guide to Matplotlib

Importing matplotlib

matplot can be imported to a python program using the following lines of code. In most of the cases, pyplot() will be used for plotting.

import matplotlib as mpl

import matplotlib.pyplot as plt

Setting Styles

when dealing with multiple datasets, in order to get better clarity from visualization, we may need to use a different variety of plots with various colors. To do these, we use plt.style directive

plt.style.use('classic')

there are a number of styles predefined in matplot that gives a  wide variety of aesthetics. A detailed list can be found here.https://matplotlib.org/gallery/style_sheets/style_sheets_reference.html

Displaying plots

Matplotlib differs depending on how you are using it. Mainly Matplotlib is used in three ways.

  • script
  • IPython terminal
  • IPython notebook.

Plotting from a script

We use the function plt.show() within a script.

Note

plt.show() should be used only once per Python session preferably at the end of the script.

 

Plotting from an IPython shell

To enable this mode, you can use the %matplotlib magic command after starting ipython

Some changes (such as modifying properties of lines that are already drawn) will not draw automatically; to force an update, use plt.draw().

Note

Using plt.show() in Matplotlib mode is not required.

Plotting from an IPython notebook

Plotting interactively within an IPython notebook can be done with the %matplotlib

IPython notebook, you also have the option of embedding graphics directly in the notebook, with two possible options:

  • %matplotlib notebook
    interactive plots embedded within the notebook
  • %matplotlib inline
    static images of your plot embedded in the notebook

Saving Figures to File

Matplotlib has the ability to save figures in a wide variety of formats. plots can be saved using plt.savefig()

Plots can be saved in the following image formats

  • ‘eps’: ‘Encapsulated Postscript’,
  • ‘jpeg’: ‘Joint Photographic Experts Group’,
  • ‘jpg’: ‘Joint Photographic Experts Group’,
  • ‘pdf’: ‘Portable Document Format’,
  • ‘pgf’: ‘PGF code for LaTeX’,
  • ‘png’: ‘Portable Network Graphics’,
  • ‘ps’: ‘Postscript’,
  • ‘raw’: ‘Raw RGBA bitmap’,
  • ‘rgba’: ‘Raw RGBA bitmap’,
  • General Matplotlib Tips | 221
  • ‘svg’: ‘Scalable Vector Graphics’,
  • ‘svgz’: ‘Scalable Vector Graphics’,
  • ‘tif’: ‘Tagged Image File Format’,
  • ‘tiff’: ‘Tagged Image File Format’
Phone: 512-539-0390
NJ Training Academy Inc , 405 Dry Gulch Bend
Cedar Park, Texas, 78613