plotting_data

py

School

Texas A&M University *

*We aren’t endorsed by this school

Course

102

Subject

Information Systems

Date

Feb 20, 2024

Type

py

Pages

2

Uploaded by KidStraw16037

Report
import matplotlib.pyplot as plt import csv from collections import defaultdict # Function to read data from the CSV file def read_weather_data(file_name): time_periods = [] max_temps = [] avg_wind_speeds = [] precipitation = [] avg_relative_humidity = [] with open(file_name, 'r') as file: reader = csv.DictReader(file) for row in reader: for key in row.keys(): if row[key] == '': row[key] ='0' time_periods.append(row['Date']) max_temps.append(float(row['Maximum Temperature (F)'])) avg_wind_speeds.append(float(row['Average Daily Wind Speed (mph)'])) precipitation.append(float(row['Precipitation (in)'])) avg_relative_humidity.append(float(row['Average Relative Humidity (%)'])) return time_periods, max_temps, avg_wind_speeds, precipitation, avg_relative_humidity # Function to plot maximum temperature and average wind speed over time def plot_temp_wind(time_periods, max_temps, avg_wind_speeds): plt.figure(1) plt.plot(time_periods, max_temps, label='Max Temperature', color='red') plt.ylabel('Max Temperature') plt.twinx() plt.plot(time_periods, avg_wind_speeds, label='Avg Wind Speed', color='blue') plt.ylabel('Avg Wind Speed') plt.xlabel('Time Period') plt.title('Max Temperature and Avg Wind Speed over Time') plt.legend() plt.show() # Function to create a histogram of average wind speed def plot_wind_histogram(avg_wind_speeds): plt.figure(2) plt.hist(avg_wind_speeds, bins=20, alpha=0.7, color='green') plt.xlabel('Average Wind Speed') plt.ylabel('Number of Days') plt.title('Histogram of Average Wind Speed') plt.show() # Function to create a scatterplot of precipitation vs average relative humidity def plot_precipitation_humidity(precipitation, avg_relative_humidity): plt.figure(3) plt.scatter(precipitation, avg_relative_humidity, alpha=0.5) plt.xlabel('Precipitation')
plt.ylabel('Avg Relative Humidity') plt.title('Precipitation vs Avg Relative Humidity') plt.show() # Function to create a bar chart for average temperature per calendar month def plot_monthly_temps(time_periods, max_temps): months_data = defaultdict(list) for i, time_period in enumerate(time_periods): month = time_period.split('/')[0] # Extracting month from the date months_data[month].append(max_temps[i]) month_labels = sorted(months_data.keys()) avg_temps = [(sum(months_data[month])) / len(months_data[month]) for month in month_labels] highest_temps = [max(months_data[month]) for month in month_labels] lowest_temps = [min(months_data[month]) for month in month_labels] plt.figure(4) plt.bar(month_labels, avg_temps, color='orange', label='Avg Temperature') plt.plot(month_labels, highest_temps, marker='o', linestyle='--', color='red', label='Highest Temp') plt.plot(month_labels, lowest_temps, marker='o', linestyle='--', color='blue', label='Lowest Temp') plt.xlabel('Month') plt.ylabel('Temperature') plt.title('Average Temperature per Calendar Month') plt.legend() plt.show() if __name__ == "__main__": file_name = "WeatherDataCLL.csv" time_periods, max_temps, avg_wind_speeds, precipitation, avg_relative_humidity = read_weather_data(file_name) plot_temp_wind(time_periods, max_temps, avg_wind_speeds) plot_wind_histogram(avg_wind_speeds) plot_precipitation_humidity(precipitation, avg_relative_humidity) plot_monthly_temps(time_periods, max_temps)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help