Visualization with Matplotlib

Hello Learners,

Being into data science field is more of a story telling thing than any other stuffs. Let me tell you through example, suppose you joined a company as a data analyst and they gave you a case study. You need to solve the problem for the local fitness gym members. You need to make a routine work intake as per each member so that they take adequate amount of water and also don’t over take nor consume less. So you begin by taking data about each members daily count of water glass they intake per week, then you collected data about their BMI, time spending in gym, distance from home, work life, etc. Now it’s time to compare data, create visual charts so that it would be easy for them to see what was their past record and how they need to improve and how to be more hydrated.

Visualization plays an important role. We’ll now take an in-depth look at the Matplotlib tool for visualization in Python.
Matplotlib is a multiplatform data visualization library built on NumPy arrays, and
designed to work with the broader SciPy stack. It was conceived by John Hunter in
2002, originally as a patch to IPython for enabling interactive MATLAB-style plotting
via gnuplot from the IPython command line. IPython’s creator, Fernando Perez, was
at the time scrambling to finish his PhD, and let John know he wouldn’t have time to
review the patch for several months. John took this as a cue to set out on his own, and
the Matplotlib package was born, with version 0.1 released in 2003. It received an
early boost when it was adopted as the plotting package of choice of the Space Tele‐
scope 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 userbase, which in turn has led to an active developer base and Matplotlib’s powerful tools and ubiquity within the scientific Python world.

  • General Matplotlib Tips
    Before we dive into the details of creating visualizations with Matplotlib, there are a
    few useful things you should know about using the package.

  • Importing matplotlib
    Just as we use the np shorthand for NumPy and the pd shorthand for Pandas, we will
    use some standard shorthands for Matplotlib imports:

In[1]: import matplotlib as mpl
import matplotlib.pyplot as plt

We will use the plt.style directive to choose appropriate aesthetic styles for our fig‐
ures. Here we will set the classic style, which ensures that the plots we create use the
classic Matplotlib style:

In[2]: plt.style.use(‘classic’)

  • show() or No show()? How to Display Your Plots
    A visualization you can’t see won’t be of much use, but just how you view your Mat‐
    plotlib plots depends on the context. The best use of Matplotlib differs depending on
    how you are using it; roughly, the three applicable contexts are using Matplotlib in a
    script, in an IPython terminal, or in an IPython notebook.

If you are using Matplotlib from within a script, the function plt.show() is your
friend. plt.show() starts an event loop, looks for all currently active figure objects,
and opens one or more interactive windows that display your figure or figures.
So, for example, you may have a file called myplot.py containing the following:

------- file: myplot.py ------

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()
You can then run this script from the command-line prompt, which will result in a
window opening with your figure displayed:

$ python myplot.py

The plt.show() command does a lot under the hood, as it must interact with your
system’s interactive graphical backend. The details of this operation can vary greatly
from system to system and even installation to installation, but Matplotlib does its
best to hide all these details from you.
One thing to be aware of: the plt.show() command should be used only once per
Python session, and is most often seen at the very end of the script. Multiple show()
commands can lead to unpredictable backend-dependent behavior, and should
mostly be avoided

  • Adjusting the Plot: Line Colors and Styles
    The first adjustment you might wish to make to a plot is to control the line colors and
    styles. The plt.plot() function takes additional arguments that can be used to specify these. To adjust the color, you can use the color keyword, which accepts a string
    argument representing virtually any imaginable color. The color can be specified in a
    variety of ways (Figure 4-9):

In[6]:
plt.plot(x, np.sin(x - 0), color=‘blue’) # specify color by name
plt.plot(x, np.sin(x - 1), color=‘g’) # short color code (rgbcmyk)
plt.plot(x, np.sin(x - 2), color=‘0.75’) # Grayscale between 0 and 1
plt.plot(x, np.sin(x - 3), color=’#FFDD44’) # Hex code (RRGGBB from 00 to FF)
plt.plot(x, np.sin(x - 4), color=(1.0,0.2,0.3)) # RGB tuple, values 0 and 1
plt.plot(x, np.sin(x - 5), color=‘chartreuse’); # all HTML color names supported

If no color is specified, Matplotlib will automatically cycle through a set of default
colors for multiple lines.
Similarly, you can adjust the line style using the linestyle keyword

In[7]: plt.plot(x, x + 0, linestyle=‘solid’)
plt.plot(x, x + 1, linestyle=‘dashed’)
plt.plot(x, x + 2, linestyle=‘dashdot’)
plt.plot(x, x + 3, linestyle=‘dotted’);

For short, you can use the following codes:

plt.plot(x, x + 4, linestyle=’-’) # solid
plt.plot(x, x + 5, linestyle=’–’) # dashed
plt.plot(x, x + 6, linestyle=’-.’) # dashdot
plt.plot(x, x + 7, linestyle=’:’); # dotted

Try these in your notebook and explore more.

Keep Coding!