Python Charts

Let’s setup two scatterplots for Greece vs the United Kingdom at once

import pandas as pd

df = pd.read_excel('europe_cities_by_sunshine.xlsx')

for country in ['Greece', 'United Kingdom']:

sub_df = df[df['Country'].isin([country])]

ax = sub_df.plot(kind='scatter', x='Jan', y='Jul' )

for idx, row in sub_df.iterrows():

ax.annotate(row['City'], (row['Jan'], row['Jul']))

ax.set_title(f'{country} Months By Total Sunshine')

Explanation:

In line 3, we’ll go through a for loop twice, once for Greece and the second time for the UK. The subdf will be a copy of the original dataframe we read in with all Europe country data, and the sub_df will only have Greece data on the first loop. Then we’ll plot this Greece chart, and we’ve now got a for loop within a for loop. This second for loop will go through the Greece rows (cities) and annotate on the graph with their labels. To further clarify, we’re adding a title at the end of the outer for loop with the country name for easier reading.