import matplotlib.pyplot as plt
%config InlineBackend.figure_formats=['svg']
import numpy as np
import pandas as pd
data_Gent = pd.read_csv('datasets/Gent_autos_inwoners.csv', decimal=',')
data_Gent['aantal inwoners'] = data_Gent['aantal inwoners'].fillna(0).apply(pd.to_numeric, downcast = 'signed')
data_Gent['Per 1000 inw'] = data_Gent['Per 1000 inw'].apply(pd.to_numeric, downcast = 'signed')
data_Gent.round({'Per 1000 inw': 0, 'jaar': 0})
#data_Gent['jaar'] = data_Gent['jaar'].fillna(0).apply(pd.to_numeric, downcast = 'signed')
data_Gent.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 29 entries, 0 to 28 Data columns (total 9 columns): jaar 27 non-null float64 Aantal auto’s 29 non-null int64 aantal inwoners 29 non-null float64 Aantal auto’s tov 2000 29 non-null int64 +/- auto’s 29 non-null int64 +/- inwoners 21 non-null float64 ratio 21 non-null float64 Per 1000 inw 22 non-null float64 Aantal inwoners tov 2000 1 non-null float64 dtypes: float64(6), int64(3) memory usage: 2.1 KB
data_Gron = pd.read_csv('datasets/Gron_autos_inwoners.csv', decimal=',')
data_Gron['Per 1000 inw'] = data_Gron['Per 1000 inw'].apply(pd.to_numeric, downcast = 'signed')
data_Gron.round({'Per 1000 inw': 0, 'jaar': 0})
data_Gron['jaar'] = data_Gron['jaar'].fillna(0).apply(pd.to_numeric, downcast = 'signed')
data_Gron.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 27 entries, 0 to 26 Data columns (total 8 columns): jaar 27 non-null int64 Aantal auto’s 27 non-null int64 aantal inwoners 24 non-null float64 Aantal auto’s tov 2000 27 non-null int64 +/- auto’s 26 non-null float64 +/- inwoners 23 non-null float64 ratio 23 non-null float64 Per 1000 inw 23 non-null float64 dtypes: float64(5), int64(3) memory usage: 1.8 KB
0 359.0 1 354.0 2 350.0 3 346.0 4 347.0 5 352.0 6 355.0 7 356.0 8 357.0 9 357.0 10 356.0 11 353.0 12 353.0 13 351.0 14 353.0 15 355.0 16 349.0 17 338.0 18 324.0 19 308.0 20 299.0 21 289.0 22 305.0 23 NaN 24 NaN 25 NaN 26 NaN Name: Per 1000 inw, dtype: float64
data_Lpzg = pd.read_csv('datasets/Leipzig_autos_inwoners.csv', decimal=',')
data_Lpzg['Per 1000 inw'] = data_Lpzg['Per 1000 inw'].apply(pd.to_numeric, downcast = 'signed')
data_Lpzg.round({'Per 1000 inw': 0, 'jaar': 0})
data_Lpzg['jaar'] = data_Lpzg['jaar'].fillna(0).apply(pd.to_numeric, downcast = 'signed')
data_Lpzg.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 20 entries, 0 to 19 Data columns (total 8 columns): jaar 20 non-null int64 Aantal auto’s 19 non-null float64 aantal inwoners 20 non-null int64 Aantal auto’s tov 2000 19 non-null float64 +/- auto’s 19 non-null float64 +/- inwoners 20 non-null int64 ratio 19 non-null float64 Per 1000 inw 19 non-null float64 dtypes: float64(5), int64(3) memory usage: 1.3 KB
0 NaN 1 392.0 2 393.0 3 397.0 4 398.0 5 398.0 6 399.0 7 383.0 8 377.0 9 372.0 10 368.0 11 366.0 12 400.0 13 398.0 14 393.0 15 389.0 16 387.0 17 385.0 18 378.0 19 383.0 Name: Per 1000 inw, dtype: float64
x = data_Gent['jaar']
y = data_Gent['Aantal auto’s tov 2000']
color = 'tab:red'
fig, ax = plt.subplots(1, 1)
ax.set_xlabel('jaar')
ax.set_ylabel('Aantal auto’s t.o.v. 2000 (2000 = 100)', color=color)
#select number of cars in 2000 in Ghent
count_2000 = data_Gent.loc[data_Gent['jaar'] == 2000]['Aantal auto’s']
label='Aantal auto’s tov 2000 in Gent {0}'.format(count_2000)
ax.plot(x, y, label=label, color=color)
x = data_Gron['jaar']
y = data_Gron['Aantal auto’s tov 2000']
color = 'tab:blue'
ax.plot(x, y, label='Aantal auto’s tov 2000 in Groningen', color=color)
x = data_Lpzg['jaar']
y = data_Lpzg['Aantal auto’s tov 2000']
color = 'tab:green'
ax.plot(x, y, label='Aantal auto’s tov 2000 in Leipzig', color=color)
plt.legend()
<matplotlib.legend.Legend at 0x116fb44a8>
x = data_Gent['jaar']
y = data_Gent['Per 1000 inw']
color = 'tab:red'
fig, ax = plt.subplots(1, 1)
ax.set_xlabel('jaar')
ax.set_ylabel('Per 1000 inw', color=color)
label='Aantal auto\'s per 1000 inw in Gent'
ax.plot(x, y, label=label, color=color)
x = data_Gron['jaar']
y = data_Gron['Per 1000 inw']
color = 'tab:blue'
ax.plot(x, y, label='Aantal auto\'s per 1000 inw in Groningen', color=color)
x = data_Lpzg['jaar']
y = data_Lpzg['Per 1000 inw']
color = 'tab:green'
ax.plot(x, y, label='Aantal auto\'s per 1000 inw in Leipzig', color=color)
plt.legend()
<matplotlib.legend.Legend at 0x11d7fd908>
#x = data_Gent['jaar']
#y = data_Gent['aantal inwoners']
# select records without Null values
first_year = data_Gent.loc[data_Gent['aantal inwoners'] > 0]['jaar'].min()
x = data_Gent.loc[data_Gent['jaar'] >= first_year]['jaar']
y = data_Gent.loc[data_Gent['aantal inwoners'] > 0]['aantal inwoners']
color = 'tab:red'
fig, ax = plt.subplots(1, 1)
ax.set_xlabel('jaar')
ax.set_ylabel('aantal inwoners', color=color)
label='Aantal inwoners in Gent'
ax.plot(x, y, label=label, color=color)
x = data_Gron['jaar']
y = data_Gron['aantal inwoners']
color = 'tab:blue'
ax.plot(x, y, label='aantal inwoners in Groningen', color=color)
x = data_Lpzg['jaar']
y = data_Lpzg['aantal inwoners']
color = 'tab:green'
ax.plot(x, y, label='aantal inwoners in Leipzig', color=color)
plt.legend()
<matplotlib.legend.Legend at 0x11def2908>
## --- --- --- --- ##
fig, (axes, axes3) = plt.subplots(2,1)
#fig, axes = plt.subplots()
color = 'tab:red'
x = data['jaar']
y = data['Aantal auto’s tov 2000']
axes.set_xlabel('jaar')
axes.set_ylabel('Aantal auto’s tov 2000', color=color)
axes.set_title('Gent');
axes.plot(x, y, label='Aantal auto’s tov 2000', color=color)
axes.tick_params(axis='y', labelcolor=color)
axes2 = axes.twinx() # instantiate a second axes that shares the same x-axis
color = 'tab:blue'
axes2.set_ylabel('Auto\'s Per 1000 inw', color=color) # we already handled the x-label with ax1
z = data['Per 1000 inw'][:22]
xx = x[:22]
#axes2.plot(xx, z, 'r--')
axes2.plot(xx, z, color=color)
axes2.tick_params(axis='y', labelcolor=color)
#axes2.set_yticks([410, 415, 420])
axes.legend()
axes2.legend()
#fig.tight_layout() # otherwise the right y-label is slightly clipped
color = 'tab:red'
#x = data['jaar']
y = data['Aantal auto’s']
z = data['aantal inwoners']
axes3.set_xlabel('jaar')
axes3.set_ylabel('Aantal auto’s', color=color)
axes3.set_title('Auto\'s en inwoners in Gent');
axes3.plot(x, y, label='Aantal auto’s', color=color)
axes3.tick_params(axis='y', labelcolor=color)
axes4 = axes3.twinx() # instantiate a second axes that shares the same x-axis
color = 'tab:blue'
axes4.set_ylabel('aantal inwoners', color=color) # we already handled the x-label with ax1
z = data['aantal inwoners']
#xx = x[:22]
#axes2.plot(xx, z, 'r--')
axes4.plot(x, z, color=color)
axes4.tick_params(axis='y', labelcolor=color)
axes3.legend()
axes4.legend()
plt.show()
x = data2['jaar']
y = data2['Aantal auto’s tov 2000']
[<matplotlib.lines.Line2D at 0x11eb19198>]
x = data3['jaar']
y = data3['Aantal personenauto’s']
fig = plt.figure(); ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y, color='r')
x = data3['Per 1000 inw']
ax.plot(x, y, 'k--')
ax.set_xticks([2000, 2005, 2007, 2010, 2015, 2017])
ax.set_xlabel('Years')
#ax.set_yticks([185000, 200000, 210000, 220000, 230000])
ax.set_ylabel('Number of cars in the city')
ax.set_title('Numbers of cars in Leipzig')
plt.show()
data4 = pd.read_csv('datasets/Verkehr_Kraftfahrzeugbestand.csv')
data4.columns
Index(['Kennziffer', 'Einheit', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017'], dtype='object')
my = data4.loc[:,'2005':'2010']
my.T.plot(legend = 'best', label = 'type', style='--') #T = swap x and y axes (transpose)
<matplotlib.axes._subplots.AxesSubplot at 0x11a43c2b0>