Data Visualization in Python

At this point in the course, it is time to begin connecting the dots and applying visualization to your knowledge of statistics. Work through these programming examples to round out your knowledge of seaborn as it is applied to univariate and bivariate plots.

Finer Control with matplotlib





https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py

As you can see from the figure, you can control each aspect of the plot displayed above using matplotlib. I won't go into the details, and will leave it to you to look at the matplotlib documentation and examples if you need to customize at this level of granularity.

The following is an example using pure matplotlib. You can see how you can build up a plot. The crucial part here is that you need to run the code from each chunk together.

from matplotlib.ticker import FuncFormatter
data = {'Barton LLC': 109438.50, 
        'Frami, Hills and Schmidt': 103569.59, 
        'Fritsch, Russel and Anderson': 112214.71, 
        'Jerde-Hilpert': 112591.43, 
        'Keeling LLC': 100934.30, 
        'Koepp Ltd': 103660.54, 
        'Kulas Inc': 137351.96, 
        'Trantow-Barrows': 123381.38, 
        'White-Trantow': 135841.99, 
        'Will LLC': 104437.60} 
group_data = list(data.values()) 
group_names = list(data.keys()) 
group_mean = np.mean(group_data) 
plt.style.use('default') 
fig, ax = plt.subplots() 
<string>:1: RuntimeWarning: More than 20 figures have been opened.
Figures created through the pyplot interface (`matplotlib.pyplot.figure`)
are retained until explicitly closed and may consume too much memory.
(To control this warning, see the rcParam `figure.max_open_warning`).
plt.show() 


fig, ax = plt.subplots()
ax.barh(group_names, group_data);
plt.show()


fig, ax = plt.subplots() 
ax.barh(group_names, group_data) 
<BarContainer object of 10 artists>
ax.set(xlim = [-10000, 140000], xlabel = 'Total Revenue', ylabel = 'Company', 
       title = 'Company Revenue'); 
plt.show() 


fig, ax = plt.subplots(figsize=(8, 4))ax.barh(group_names, group_data)
<BarContainer object of 10 artists>
labels = ax.get_xticklabels()
plt.setp(labels, rotation=45, horizontalalignment='right')
[None, None, None, None, None, None, None, None, None, None,
 None, None, None, None, None, None, None, None]
ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company', 
       title='Company Revenue'); 
plt.show() 


After you have created your figure, you do need to save it to disk so that you can use it in your Word or Markdown or PowerPoint document. You can see the formats available.

fig.canvas.get_supported_filetypes()
{'ps': 'Postscript', 'eps': 'Encapsulated Postscript',
 'pdf': 'Portable Document Format', 'pgf': 'PGF code for LaTeX',
 'png': 'Portable Network Graphics', 'raw': 'Raw RGBA bitmap',
 'rgba': 'Raw RGBA bitmap', 'svg': 'Scalable Vector Graphics',
 'svgz': 'Scalable Vector Graphics', 'jpg': 'Joint Photographic Experts Group',
 'jpeg': 'Joint Photographic Experts Group', 'tif': 'Tagged Image File Format',
 'tiff': 'Tagged Image File Format'}

The type will be determined by the ending of the file name. You can add some options depending on the type. I'm showing an example of saving the figure to a PNG file. Typically I'll save figures to a vector graphics format like PDF, and then convert into other formats, since that results in minimal resolution loss. You of course have the option to save to your favorite format.

 # fig.savefig('sales.png', dpi = 300, bbox_inches = 'tight') 


Matlab-like plotting

matplotlib was originally developed to emulate Matlab. Though this kind of syntax is no longer recommended, it is still available and may be of use to those coming to Python from Matlab or Octave.

import matplotlib.pyplot as plt 
plt.plot([1, 2, 3, 4]); 
plt.ylabel('some numbers'); 
plt.show() 


import numpy as np 
# evenly sampled time at 200ms intervals 
t = np.arange(0., 5., 0.2) 
# red dashes, blue squares and green triangles 
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^'); 
plt.show()


def f(t): 
    return np.exp(-t) * np.cos(2*np.pi*t) 
t1 = np.arange(0.0, 5.0, 0.1) 
t2 = np.arange(0.0, 5.0, 0.02) 
plt.figure(); 
<string>:1: RuntimeWarning: More than 20 figures have been opened.
 Figures created through the pyplot interface (`matplotlib.pyplot.figure`)
 are retained until explicitly closed and may consume too much memory.
 (To control this warning, see the rcParam `figure.max_open_warning`).
plt.subplot(211); 
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k'); 
plt.subplot(212); 
plt.plot(t2, np.cos(2*np.pi*t2), 'r--'); 
plt.show()