Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_12/python-notebooks-data-wrangling/Basic visualization concepts in Matplotlib.ipynb
1904 views
Kernel: Python 3

Draft Contents

Making a chart Saving to file

Changing the characteristics of the line - color - width - pattern

Creating different chart types

Adjusting the axes

  • Changing the limits

  • Labeling the axes

  • Changing tick frequency

Changing the background

Annotating the chart points

Labeling the data and the chart

Changing spacing

Bucketing

Loading the data

%matplotlib inline import matplotlib.pyplot as plt # only necessary for notebook mode from csv import DictReader from os import makedirs from os.path import join DATA_FILENAME = join('data', 'climate', 'extracted', 'nasa-ghgases-co2-mean.csv') IMAGES_DIR = join('images', 'climate') makedirs(IMAGES_DIR, exist_ok=True)
# loading the data with open(DATA_FILENAME, 'r') as f: data = list(DictReader(f))

Here's what the data looks like; because the CSV text format does not indicate whether '285.2' is intended to be a float or a string literal, the data deserializes as a bunch of strings:

data[0:5]
[{'co2_ppm_mean': '285.2', 'year': '1850'}, {'co2_ppm_mean': '285.1', 'year': '1851'}, {'co2_ppm_mean': '285.0', 'year': '1852'}, {'co2_ppm_mean': '285.0', 'year': '1853'}, {'co2_ppm_mean': '284.9', 'year': '1854'}]

As it turns out, MPL's plotting handles the numerical-values-as-strings without a hitch (for now):

years = [d['year'] for d in data] co2ppm = [d['co2_ppm_mean'] for d in data] plt.plot(years, co2ppm);
Image in a Jupyter notebook

Axes limits

plt.plot(years, co2ppm); plt.xlim(1860, 1900) # bucket it
(1860, 1900)
Image in a Jupyter notebook
plt.ylim(ymin=0, ymax=max([float(i) for i in co2ppm])) plt.xlim(1900, 2000)
(1900, 2000)
Image in a Jupyter notebook
# TKTKTK