Wednesday, August 2, 2017

Python Weather Temperature Plot

# import the usual
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# open the file
my_file = pd.read_csv(r'F:\Python\weather_data.txt', sep='\t',
               header=0, low_memory=False, encoding='latin-1')

max_temp = my_file['Max TemperatureF'].tolist()
min_temp = my_file['Min TemperatureF'].tolist()
day_of_year = [i+1 for i in range(len(my_file))]

x = range(len(my_file))
y = range(np.min(min_temp)+5, np.max(max_temp)+5)
z = [[z] * len(my_file) for z in range(len(y))]
num_bars = 500

fig, ax = plt.subplots(figsize=(10,7), dpi=300, frameon=False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)

ax.yaxis.grid(b=False, which='major', color='gray', linestyle='-.')
ax.xaxis.grid(b=False, which='major', color='gray', linestyle='-.')

contourf = ax.contourf(x, y, z, num_bars, cmap=plt.cm.jet)

#plt.grid(alpha=0.5)
max_temp_plt = ax.plot(day_of_year, max_temp, label='high', lw=1, color='grey')
min_temp_plt = ax.plot(day_of_year, min_temp, label = 'low', lw=1, color='grey')
ax.set_xlabel('Day of Year', fontsize=20)
ax.set_ylabel('Temperature (F)', fontsize=20)
ax.set_title('Temperatures Plot', fontsize=22)
ax.set_xlim(1, len(my_file))
ax.set_ylim(np.min(min_temp)+5, np.max(max_temp)+5)

ftwn_min_temp = ax.fill_between(day_of_year, 0.0, min_temp, 
                                 alpha=0.999, color='white')
ftwn_max_temp = ax.fill_between(day_of_year, max_temp, np.max(max_temp)+20, 
                                 alpha=0.999, color='white')

plt.show()
Data is available here.

No comments:

Post a Comment