This example is a simple tutorial on how to plot data in Python using matplotlib. For this tutorial and beyond, it is recommended that you have the Anaconda Python distribition, which is free from Continuum Analytics. Link: https://store.continuum.io/cshop/anaconda/

Concepts:

- Importing Packages
- Variables and Data Types
- Simple plotting

IMPORTS:

In this example, we will be using two external packages (which come installed by default with Anaconda). We need to import these packages so that we can use them in the example.


In [1]:
## this is how you import a package. You could just do:
## import numpy
## import matplotlib.pyplot
## however, it would become tiresome to type all of that
## every time we wanted to call a function. So, we import them
## as shorter names, such as np or plt.
import numpy as np
import matplotlib.pyplot as plt

Some programs will also use the syntax

from numpy import *

What this does is import every function from the package numpy. The down side to doing this is that it makes it unclear where the function you are using is coming from. Therefore, it tends to be better practice to do it as above.

VARIABLES AND DATA TYPES:

In computing, variables are how we store information. In Python, this information can be represented in 3 basic ways: Integers: Also referred to a whole numbers(e.g. 1, 2, 3, etc) Floats: Numbers with decimal points (e.g. 3.14159) Strings: A series of text character (e.g. "Hello, world!")

These data types can be stored in various formats...

As a single variable (e.g. x = 2)
As a list/array (e.g. x = [1,2,3,4,5])
As a dictionary (e.g. x = { key: val }
As a tuple (e.g. x = (1,2))

In this example, we will be using lists to store data in a series. In this case, the data will be ordered by time. We will be creating a basic meteogram. A meteogram is used to visualize surface weather observations over a period of time, ranging from hours to days. It can be used to display temperature, wind, dewpoint... anything that a surface weather observation station records over a period of time. An excellent example of meteogram can be seen on the Oklahoma Mesonet: http://www.mesonet.org/index.php/weather/meteogram/nrmn


In [2]:
## We will create an arbitray list of temperature values to work with in this demo.
## Lists are created by placing comma separated values inside of square brackets [] 
## following an equal sign and a variable name. In this case, we are creating a list
## of temperatures in degrees F.
tmpf = [72.0, 84.0, 93.2, 87.0, 76.0]

## dewpoint temperature in degrees F
dwpf = [66.0, 63.0, 61.0, 62.3, 62.5]

## these are the times of each observation in 24 hour format
time = [6,  10, 14, 18, 22]

To handle the plotting of the data, we will be using the matplotlib library. There are fantastic demos of the plotting capabilities in the matplotlib example gallery: http://matplotlib.org/gallery.html

For the purpose of this demo, we will be doing a simple line plot for temperature and dewpoint, using time as the X axis, and temperature in degrees F as the Y axis. The red line will be for temperature, and the green line will be for dewpoint temperature.


In [3]:
## plt.plot will plot a line, with the first argument being the x axis, 
## the second being the Y axis value, and the third being the type of plot.
## In this case, 'r-' means red line, and 'g-' means green line. More info
## can be found in the matplotlib documentation.
plt.plot(time, tmpf, 'r-')
plt.plot(time, dwpf, 'g-')
## you must call plt.show() if you want to see your plot!
plt.show()

In [ ]: