Plot 2 different variables using 2 y-axis (from Satlantic’s LOBO server)

Here we will make a plot using 2 different dependent variables with different scales. Satlantic LOBO is a ocean observatory moored in the North West Arm (Halifax, Nova Scotia, Canada). In this post, we’ll query the LOBO server to create a temperature and salinity plot.




import urllib2
import StringIO
import csv
import matplotlib.pyplot as plt

#define start and end dates
startdate = '20121120'
enddate = '20121130'
# Read data from LOBO
response = urllib2.urlopen(''
data =
# We use StringIO to convert data into a StringIO object
# Learn more:
data = StringIO.StringIO(data)
# read the StringIO object as it was a file
r = csv.DictReader(data,

# Put the values into x, y1, and y2.
date= []
temp= []
salt= []
for row in r:
    date.append(row['date [AST]'])
    temp.append(row['temperature [C]'])
# Change the time strings into datetime objects
from datetime import datetime
DAT = []
for row in date:
    DAT.append(datetime.strptime(row,"%Y-%m-%d %H:%M:%S"))
#create figure
fig, ax =plt.subplots(1)
# Plot y1 vs x in blue on the left vertical axis.
plt.xlabel("Date [AST]")
plt.ylabel("Temperature [C]", color="b")
plt.tick_params(axis="y", labelcolor="b")
plt.plot(DAT, temp, "b-", linewidth=1)
plt.title("Temperature and Salinity from LOBO (Halifax, Canada)")

# Plot y2 vs x in red on the right vertical axis.
plt.ylabel("Salinity", color="r")
plt.tick_params(axis="y", labelcolor="r")
plt.plot(DAT, salt, "r-", linewidth=1)
#To save your graph

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s