Multiline temperature plot (Python MatPlot)

vegaseat 1 Tallied Votes 326 Views Share

I used csv data readily available from http://www.weatherdatadepot.com/
to create a multiline plot of the average monthly temperatures of a given location.

''' mp_lineplot101F.py
multiline temperature plot of years 2010 - 2014
using the Python MatPlot module

black line --> 'k'
red line --> 'r'
blue line --> 'b'  (default)
green line --> 'g'
red dashes --> 'r--'
blue squares --> 'bs'
blue dots and dashes --> 'bo--'  or  'o--'  (default is blue)
red dots and dashes --> 'ro--'
green triangles --> 'g^'


csv data from http://www.weatherdatadepot.com/ ...
Average monthly temperatures (degF) for Ann Arbor Michigan:
----------------------------------------------------
Year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
2010,23,25,40,51,60,68,73,72,61,52,39,24
2011,19,22,33,46,59,67,75,69,61,50,43,33
2012,29,31,48,48,63,70,77,69,60,50,37,33
2013,25,25,32,44,60,66,70,68,60,50,36,25
2014,13,14,26,47,58,67,66,69,59,49,33,32

tested with Python34  by  vegaseat  04feb2015
'''

import matplotlib.pyplot as plt

# optional font
font = {'fontname'   : 'Bitstream Vera Sans',
        'color'      : 'r',
        'fontweight' : 'bold',
        'fontsize'   : 12}

# x axis data list
xd = range(1, 13)

months = [m for m in "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec".split(',')]
#print(months)  # test

# y axis data
str_2010 = "23,25,40,51,60,68,73,72,61,52,39,24"
# convert to list of integers
temps_2010 = [int(t) for t in str_2010.split(',')]
#print(temps_2010)   # test
str_2011 = "19,22,33,46,59,67,75,69,61,50,43,33"
temps_2011 = [int(t) for t in str_2011.split(',')]
str_2012 = "29,31,48,48,63,70,77,69,60,50,37,33"
temps_2012 = [int(t) for t in str_2012.split(',')]
str_2013 = "25,25,32,44,60,66,70,68,60,50,36,25"
temps_2013 = [int(t) for t in str_2013.split(',')]
str_2014 = "13,14,26,47,58,67,66,69,59,49,33,32"
temps_2014 = [int(t) for t in str_2014.split(',')]

width = 5
plt.title('Average monthly temperatures (degF) for Ann Arbor Michigan', font)
plt.plot(xd, temps_2010, 'b', label="2010")
plt.plot(xd, temps_2011, 'r', label="2011")
plt.plot(xd, temps_2012, 'g', label="2012")
plt.plot(xd, temps_2013, '^--', label="2013")
plt.plot(xd, temps_2014, 'ro--', label="2014")
plt.ylabel('degF')
plt.xlabel('Months')
plt.grid(True)
# plt.legend(loc=(x_pos, y_pos))
plt.legend(loc=(0.03, 0.6))
# replace numeric list xd with month name list
plt.xticks(xd, months)

# optional save
# default is .png image file
plt.savefig('AA_monthly_temps', dpi=50)

plt.show()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.