03.python.08.plot.examples
03.python.08.plot.examples
Python
pandas and matplotlib
line plot
multiple line plots
barplot
boxplot
heatmap
FDATA = "./files/matplot.life-expectancy-vs-health-expenditure.csv"
OPTCHART = {1: ['Life Expectancy', 'LifeExpectancy','Number of years'], 2: ['Health
Expenditure','HealthExpenditure','Dollars/Capita Year']}
COLORS = {"LifeExpectancy": "#16acd8", "HealthExpenditure": "#4e16d8"}
COLRENAME = {'Entity':'Country'}
def menu():
strOut = ""
for key, val in OPTCHART.items():
strOut += str(key) + ": " + val[0] + "\n"
strOut += "Your choice: "
return strOut
print(menu())
df = pd.read_csv(FDATA)
df = df.rename(columns=COLRENAME)
#print(df.head())
# list of countries to check user's input
clist = df.Country.unique()
selcol = {}
selcol[colname] = COLORS[colname]
pandas plot
matplotlib
fig, ax = plt.subplots()
plt.plot(dfsel['Year'], dfsel[colname], label=OPTCHART[opt][0]) # Set label for first
line
plt.xlabel('Year')
plt.ylabel(OPTCHART[opt][2])
plt.title("matplotlib: " + OPTCHART[opt][0] + "\n" + country.title())
plt.legend() # Show legend with the specified labels
#plt.show()
create table with column per country and plot with single chart
matplotlib
# single char
fig, axes = plt.subplots()
for i, country in enumerate(countries):
plt.plot(dftab.index, dftab[country], label=country) # Plot each entity
plt.xlabel('Year')
plt.ylabel(OPTCHART[opt][2])
plt.title("matplotlib - single chart: " + OPTCHART[opt][0])
plt.legend(title='Country')
plt.show()
ncountries = len(countries)
fig, axes = plt.subplots(ncountries, 1, figsize=(8, 6 * ncountries))
for i, country in enumerate(dftab.columns):
axes[i].plot(dftab.index, dftab[country], color=COLORS[i], legend=False)
axes[i].set_title(f'{country} Life Expectancy Over Years')
axes[i].set_xlabel('Year')
axes[i].set_ylabel(OPTCHART[opt][2])
plt.tight_layout()
plt.show()
Q4 boxplot
selcountries = ['France','Germany','Italy']
df.groupby('Year').agg({'LifeExpectancy':'mean'})
dfcs = df[(df['Year']>2000) & (df['Country'].isin(selcountries))]
[['Country','Year','LifeExpectancy','HealthExpenditure']]
#dfcs.groupby('Country')[['LifeExpectancy']].boxplot()
ax = dfcs.groupby('Country')[['LifeExpectancy']].boxplot(subplots=False)
ax.set_xticklabels(selcountries)
Passwords
FDATA = "./files/passwords.txt.csv"
FCAT = "./files/passwords.cat.csv"
dfp = pd.read_csv(FDATA)
dfc = pd.read_csv(FCAT)
dfp.head()
dfc.head()
dfp['online_hours'] = dfp['value']*dfp['time_unit'].map(TIMECONV)
dfp['online_hours'] = dfp['online_hours'].astype(int)
#dfp[dfp['online_hours'].isna()]
matplotlib
fig, ax = plt.subplots()
ax.bar(dfres['category'], dfres['online_hours'], width=1, edgecolor="white",
linewidth=0.7)
###
ncat = dfres['category'].nunique()
ax.set_xlim(-1,ncat)
###
plt.xticks(rotation=90)
plt.title('Matplot on grouped')
plt.show()
histogram
dfpc[['strength']].plot(kind='hist')