SENCE 2023 Examples¶
Common libraries and parameters¶
In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [2]:
# Don't show too many rows in Pandas Dataframes
pd.options.display.max_rows = 7
In [3]:
# Larger plots
plt.rcParams['figure.figsize'] = [16, 8]
In [4]:
# "pip install folium" might be needed first : https://pypi.org/project/folium/
import folium
In [5]:
# Make a data frame with dots to show on the map.
# All the values are the same, in order to check if the projection distorts the circles
data = pd.DataFrame({
'lon':[-58, 2, 145, 30.32, -4.03, -73.57, 36.82, -38.5],
'lat':[-34, 49, -38, 59.93, 5.33, 45.52, -1.29, -12.97],
'name':['Buenos Aires', 'Paris', 'Melbourne', 'St Petersbourg', 'Abidjan', 'Montreal', 'Nairobi', 'Salvador'],
'value': [50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0]
})
data
Out[5]:
Circles are distorted by Mercator projection¶
see https://en.wikipedia.org/wiki/Tissot%27s_indicatrix for more information
In [6]:
# Make an empty map
m = folium.Map(location=[20,0], tiles="OpenStreetMap", zoom_start=2)
# add marker one by one on the map
for city in data.itertuples():
folium.Circle(
location=[city.lat, city.lon],
popup=city.name,
radius=city.value * 20000.0,
color='crimson',
fill=True,
fill_color='crimson'
).add_to(m)
m.get_root().html.add_child(folium.Element("<h3 align='center'>Map with distorted circles</h3>"))
# Show the map
m
Out[6]:
Avoiding deformation¶
In [7]:
import math
m = folium.Map(location=[20,0], tiles="OpenStreetMap", zoom_start=2)
# add marker one by one on the map, and account for Mercator deformation
for city in data.itertuples():
local_deformation = math.cos(city.lat * math.pi / 180)
folium.Circle(
location=[city.lat, city.lon],
popup='%s (%.1f)' % (city.name, city.value),
radius=city.value * 20000.0 * local_deformation,
color='crimson',
fill=True,
fill_color='crimson'
).add_to(m)
m.get_root().html.add_child(folium.Element("<h3 align='center'>Map with circles of correct size</h3>"))
m.save('Output/bubble_map.html')
m
Out[7]:
Basic example¶
In [8]:
# initialize columns
data = {
'A': [0, 1, 2, 3, 4, 5, 6],
'B': [1, 2, 3, 4, 5, 6, 7],
'C': [2, 3, 4, 5, 6, 7, 8],
'D': [3, 4, 5, 6, 7, 8, 9],
'E': [4, 5, 6, 7, 8, 9, 10],
'F': [5, 6, 7, 8, 9, 10, 11]
}
df = pd.DataFrame(data)
In [9]:
df
Out[9]:
In [10]:
colors = 'viridis' # See https://matplotlib.org/stable/gallery/color/colormap_reference.html
sns.heatmap(df, cmap=colors)
plt.title("Heatmap from pandas dataframe, with '%s' colormap." % colors)
plt.show()
Heatmap from timeseries¶
In [11]:
# Parse a whole year of weather data
weather_df = pd.read_csv('Output/SchPark01.csv',
sep = ';',
na_values = ' ',
names = ['date', 'time', 'ghi', 'ta'],
parse_dates = [[0, 1]],
index_col = 'date_time'
)
weather_df
Out[11]:
In [12]:
# Temperatures(day_of_year, time)
temperatures = pd.pivot_table(weather_df, values='ta', index=weather_df.index.time, columns=weather_df.index.dayofyear)
temperatures
Out[12]:
In [13]:
sns.heatmap(temperatures, annot=False)
plt.title('Temperatures in Scharnhauser Park, 2011')
plt.show()
In [14]:
# What are the available datasets?
', '.join(sns.get_dataset_names())
Out[14]:
In [15]:
penguins_df = sns.load_dataset('penguins')
penguins_df
Out[15]:
In [16]:
# Basic correlogram
sns.pairplot(penguins_df, hue='species')
plt.show()
FlapPyBird¶
Slightly modified version of FlapPyBird, with high score file and plot if desired:
In [17]:
high_score_filename = 'Output/my_high_score.csv'
In [18]:
# Find the best score, without any library
previous_record = 0
with open(high_score_filename) as high_score_file:
for line in high_score_file:
when, old_score = line.split(';')
old_score = int(old_score)
if old_score > previous_record:
previous_record = old_score
print("Current best score is : %d" % previous_record )
In [19]:
# Parse high score file with Pandas
high_score_df = pd.read_csv(high_score_filename,
sep=';',
names=['datetime', 'score'],
parse_dates=True,
index_col='datetime')
high_score_df
Out[19]:
In [20]:
high_score_df.plot(ylim=(0, None),
title='My FlapPyBird scores',
use_index=False,
xlabel='Attempt #')
plt.show()