<jupyter_start><jupyter_text>WeatherPy---- Note* Instructions have been included for each segment. You do not have to follow them exactly, but they are included to help you think through the steps.<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests
import time
from scipy.stats import linregress

# Import API key
from api_keys import weather_api_key


# Incorporated citipy to determine city based on latitude and longitude
from citipy import citipy

# Output File (CSV)
output_data_file = "../output_data/cities.csv"

# Range of latitudes and longitudes
lat_range = (-90, 90)
lng_range = (-180, 180)

#Personal imports.
from pprint import pprint
from matplotlib.ticker import FuncFormatter
from matplotlib.ticker import Formatter

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Generate Cities List<jupyter_code># List for holding lat_lngs and cities
lat_lngs = []
cities = []

# Create a set of random lat and lng combinations
lats = np.random.uniform(low=-90.000, high=95.000, size=1600)
lngs = np.random.uniform(low=-181.000, high=181.000, size=1600)
lat_lngs = zip(lats, lngs)

# Identify nearest city for each lat, lng combination
for lat_lng in lat_lngs:
    city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name
    
    # If the city is unique, then add it to a our cities list
    if city not in cities:
        cities.append(city)

# Print the city count to confirm sufficient count
len(cities)<jupyter_output><empty_output><jupyter_text>Perform API Calls* Perform a weather check on each city using a series of successive API calls.* Include a print log of each city as it'sbeing processed (with the city number and city name).<jupyter_code>#lits for holding the query information and to keep track of the records and sets.
record = 1
Set = 1
city_df = []
cloudiness = []
country = []
date = []
humidity = []
lat = []
lng = []
maxTemp = []
windSpeed = []

#Creates header of the data retrieval log. 
s = '-' * 30
print(f'Beginning Data Retrieval\n{s}')

#For each city in the list of randome cities .
for city in cities:
    
    #If the record is equal to 50 then reset record to 1 and add 1 to the set.
    if record == 50:
        Set += 1
        record = 1
    #if the record is less than 50 then try.
    try:
        #Query url for each city.
        query_url = f"http://api.openweathermap.org/data/2.5/weather?appid={weather_api_key}&units=Imperial&q={city}"
        #Data in json format for each city.
        weather_data = requests.get(query_url).json()
        
        #Appends data to each of the empty lists created earlier.
        city_df.append(weather_data['name'])
        cloudiness.append(weather_data['clouds']['all'])
        country.append(weather_data['sys']['country'])
        date.append(pd.to_datetime(weather_data['dt'], unit = 's')) #Converts Unix data to datetime
        humidity.append(weather_data['main']['humidity'])
        lat.append(weather_data['coord']['lat'])
        lng.append(weather_data['coord']['lon'])
        maxTemp.append(weather_data['main']['temp_max'])
        windSpeed.append(weather_data['wind']['speed'])
        
        #Prints a log in the command line.
        print(f'Processing Record {record} of set {Set} | {city.title()}')
        
        #When eveything is done, adds a 1 to the record tally.
        record += 1
    
    #If data is not found for the desired city, then retur this message.
    except:
        print('City not found. Skipping...')
        
#End of the log.      
print(f'{s}\nData Retrieval Complete\n{s}')<jupyter_output>Beginning Data Retrieval
------------------------------
Processing Record 1 of set 1 | Maldonado
Processing Record 2 of set 1 | Hushitai
Processing Record 3 of set 1 | Saldanha
Processing Record 4 of set 1 | Tanabe
Processing Record 5 of set 1 | Asha
Processing Record 6 of set 1 | Ancud
Processing Record 7 of set 1 | Aklavik
Processing Record 8 of set 1 | Avera
Processing Record 9 of set 1 | Mahebourg
Processing Record 10 of set 1 | Bredasdorp
Processing Record 11 of set 1 | Mataura
Processing Record 12 of set 1 | Tasiilaq
Processing Record 13 of set 1 | Qaanaaq
Processing Record 14 of set 1 | Thompson
Processing Record 15 of set 1 | Avarua
Processing Record 16 of set 1 | Lorengau
Processing Record 17 of set 1 | Ponta Do Sol
Processing Record 18 of set 1 | Fairbanks
Processing Record 19 of set 1 | Portland
Processing Record 20 of set 1 | Hobart
Processing Record 21 of set 1 | Kapaa
Processing Record 22 of set 1 | Tabou
Processing Record 23 of set 1 | Cape Town
Processing Record 24 of s[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>#Dictionary to store all the lists.
city_dict = {
    'City' : city_df,
    'Cloudiness' : cloudiness,
    'Country' : country,
    'Date' : date,
    'Humidity' : humidity,
    'Lat' : lat,
    'Lng' : lng,
    'Max Temp' : maxTemp,
    'Wind Speed' : windSpeed
}

#Creates DataFrame from the dictionary.
weatherPy_df = pd.DataFrame(city_dict)

#Check for row total.
weatherPy_df.count()

#Check for data type. 
weatherPy_df.dtypes




#Format datetime to show only year, month and day.
weatherPy_df['Date'] = weatherPy_df['Date'].dt.strftime('%d-%m-%Y')  

weatherPy_df.head()

#Csv output.
weatherPy_df.to_csv(output_data_file, index = False)<jupyter_output><empty_output><jupyter_text>Plotting the Data* Use proper labeling of the plots using plot titles (including date of analysis) and axes labels.* Save the plotted figures as .pngs.  Latitude vs. Temperature Plot<jupyter_code>#Takes the data of our query. 
date = weatherPy_df.loc[0,'Date']

#Creates a figure with 4 plots.
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4,1, figsize = (20,20))
fig.subplots_adjust(top=0.95) #Controls the space between the title and the figure.

#Title of the plot with the date.
fig.suptitle(f'Weather Analysis {date}', fontsize = 30)

#Plot 1
ax1.scatter(weatherPy_df['Lat'],
            weatherPy_df['Max Temp'],
           facecolors = 'lightskyblue',
           edgecolors = 'black')
ax1.set_ylabel('Max Temperature' , fontsize = 15)
ax1.set_xlabel('Latitude' , fontsize = 15)
ax1.tick_params(axis='y',
                which='major',
                labelsize = 20)
ax1.tick_params(axis='x',
                which='major',
                labelsize = 15)
ax1.yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:.0f}F'.format(y))) #Formats y axis 
ax1.grid()

#Plot 2
ax2.scatter(weatherPy_df['Lat'],
            weatherPy_df['Humidity'],
           facecolors = 'lightskyblue',
           edgecolors = 'black')
ax2.set_ylabel('Humidity' , fontsize = 15)
ax2.set_xlabel('Latitude' , fontsize = 15)
ax2.tick_params(axis='y',
                which='major',
                labelsize = 20)
ax2.tick_params(axis='x',
                which='major',
                labelsize = 15)
ax2.yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:.0f}%'.format(y))) #Formats y axis 
ax2.grid()

#Plot 3
ax3.scatter(weatherPy_df['Lat'],
            weatherPy_df['Cloudiness'],
           facecolors = 'lightskyblue',
           edgecolors = 'black')
ax3.set_ylabel('Cloudiness' , fontsize = 15)
ax3.set_xlabel('Latitude' , fontsize = 15)
ax3.tick_params(axis='y',
                which='major',
                labelsize = 20)
ax3.tick_params(axis='x',
                which='major',
                labelsize = 15)
ax3.yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:.0f}%'.format(y))) #Formats y axis 
ax3.grid()

#Plot 4
ax4.scatter(weatherPy_df['Lat'],
            weatherPy_df['Wind Speed'],
           facecolors = 'lightskyblue',
           edgecolors = 'black')
ax4.set_ylabel('Wind Speed' , fontsize = 15)
ax4.set_xlabel('Latitude' , fontsize = 15)
ax4.tick_params(axis='y',
                which='major',
                labelsize = 20)
ax4.tick_params(axis='x',
                which='major',
                labelsize = 15)
ax4.yaxis.set_major_formatter(FuncFormatter(lambda y, _: '{:.0f} mph'.format(y))) #Formats y axis 
ax4.grid()

plt.show()

#Saves the plots.
fig.savefig("../output_data/latitudePLots.png")<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code># Create Northern and Southern Hemisphere DataFrames
north_df = weatherPy_df[weatherPy_df["Lat"]>=0]
south_df = weatherPy_df[weatherPy_df["Lat"]<0]

#List of the paramters used to compare against latitude.
comparison = ['Max Temp', 'Humidity', 'Cloudiness','Wind Speed']
#Data distribution.
NorthernPct = len(north_df) / len(weatherPy_df) *100

NorthernPct

#Data distribution.
SouthernPct = len(south_df) / len(weatherPy_df) * 100

SouthernPct<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>#Date of the query.
date = weatherPy_df.loc[0,'Date']

#Creates a figure with 4 plots.
fig, axs = plt.subplots(4, 1, figsize=(40,30))
cols_level = [f'ax_{i}' for i in range(len(axs.flat))] #1-D iterator to iterate over the array axs.

#Title
fig.suptitle(f'Northern Hemisphere Weather Analysis {date}', fontsize = 30)
fig.subplots_adjust(top=0.95) #Controls the space between the title and the plots.

#For loop to create plots. 
for i, ax in enumerate(axs.flat):
    
    #Data for the x and y
    x_values = north_df['Lat']
    y_values = north_df[comparison[i]]
    
    #Linear regression formula.
    (slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
    regress_values = x_values * slope + intercept
    
    #Line equation text and correlation factor.
    line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2)) + "\nThe r-squared is: " + str(round(rvalue,4))

    #Scatter plot. 
    ax.scatter(x_values,
               y_values,
               facecolors = 'lightskyblue',
               edgecolors = 'black')

    #Line plot - by plotting points of x values long the regressvalues. 
    ax.plot(x_values,regress_values,"r-")
    ax.annotate(line_eq,
             (0,-8), #location of the line_eq
             fontsize = 15,
             color = "red")

    #label for x and y axis.
    ax.set_xlabel('Latitude',fontsize = 15)
    ax.set_ylabel(comparison[i],fontsize = 25)
     
    #lim of y
    ax.set_ylim(-10,north_df[comparison[i]].max()+5)
    
    #Creates grid for each plot.
    ax.grid()
    
    #Selects the format for y based on the plot.
    if comparison[i] == 'Max Temp':
        formatAxis = '{:.0f}F'
        
    elif comparison[i] == 'Wind Speed':
        formatAxis = '{:.0f} mph'
        
    else:
        formatAxis = '{:.0f}%'
    
    #Applies the format to the y axis.
    ax.set_yticklabels([formatAxis.format(int(x)) for x in ax.get_yticks().tolist()])
    
    #Controls the size of the x and y ticks.
    ax.tick_params(axis='y',
                which='major',
                labelsize = 20)
    ax.tick_params(axis='x',
                which='major',
                labelsize = 13)

plt.show()

#Saves the plot. 
fig.savefig("../output_data/NorthernPLots.png")<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>#Date
date = weatherPy_df.loc[0,'Date']

#Creates a figure with 4 plots.
fig, axs = plt.subplots(4, 1, figsize=(40,30))
cols_level = [f'ax_{i}' for i in range(len(axs.flat))]

#Title
fig.suptitle(f'Southern Hemisphere Weather Analysis {date}', fontsize = 30)
fig.subplots_adjust(top=0.95)

#For loop to create plots. 
for i, ax in enumerate(axs.flat):
    
     #Data for the x and y
    x_values = south_df['Lat']
    y_values = south_df[comparison[i]]
    
    #Linear regression formula.
    (slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
    regress_values = x_values * slope + intercept
    
    #Line equation text.
    line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2)) + "\nThe r-squared is: " + str(round(rvalue,4))

    #Scatter plot. 
    ax.scatter(x_values,
               y_values,
               facecolors = 'lightskyblue',
               edgecolors = 'black')

    #Line plot - by plotting points of x values long the regressvalues. 
    ax.plot(x_values,regress_values,"r-")
    ax.annotate(line_eq,
             (-50,-8),
             fontsize = 15,
             color = "red")
    #label for x and y axis.
    ax.set_xlabel('Latitude',fontsize = 15)
    ax.set_ylabel(comparison[i],fontsize = 25)
    
    #lim of y
    ax.set_ylim(-10,north_df[comparison[i]].max()+5)
    
    #Grid for each plot.
    ax.grid()
    
    #Selects the format for y based on the plot.
    if comparison[i] == 'Max Temp':
        formatAxis = '{:.0f}F'
        
    elif comparison[i] == 'Wind Speed':
        formatAxis = '{:.0f} mph'
        
    else:
        formatAxis = '{:.0f}%'
    
    #Applies the format to the y axis.
    ax.set_yticklabels([formatAxis.format(int(x)) for x in ax.get_yticks().tolist()])
    
    #Controls the size of the x and y ticks.
    ax.tick_params(axis='y',
                which='major',
                labelsize = 20)
    ax.tick_params(axis='x',
                which='major',
                labelsize = 13)

plt.show()

#Saves the plot.
fig.savefig("../output_data/SouthernPLots.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11950301204819277, 'fraction_numerical': 0.021460843373493976, 'mean_word_length': 3.102873030583874, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '84477', 'license': 'ADSL', 'n_tokens_mistral': 4744, 'n_tokens_neox': 4358, 'n_words': 1273, 'path': 'WeatherPy/WeatherPy.ipynb', 'repo_name': 'darrenluc93/python-api-challenge', 'size': '13280'}