text_plain_output_1.png
from matplotlib.patches import ConnectionPatch

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

monkeypox = pd.read_csv('../input/worldwide-monkeypox-daily-dataset/owid-monkeypox-data.csv')

monkeypox.replace('Congo', 'Democratic Republic of Congo', inplace=True)
monkeypox_countr = monkeypox[monkeypox['location'] != 'World'].copy()

def get_continent_country(x):
    NorthAmerica = ['Barbados', 'Bermuda', 'Canada', 'Greenland', 'Jamaica', 'Mexico', 'Panama', 'Costa Rica', 'Dominican Republic', 'Guatemala', 'Puerto Rico', 'United States', 'Bahamas', 'Cuba', 'Honduras']
    SouthAmerica = ['Argentina', 'Bolivia', 'Brazil', 'Chile', 'Colombia', 'Ecuador', 'Peru', 'Uruguay', 'Venezuela', 'Guyana', 'Paraguay']
    Europe = ['Andorra', 'Austria', 'Belgium', 'Bosnia and Herzegovina', 'Bulgaria', 'Croatia', 'Czechia', 'Denmark', 'Estonia', 'Finland', 'France', 'Georgia', 'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland', 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Moldova', 'Montenegro', 'Netherlands', 'Norway', 'Poland', 'Portugal', 'Romania', 'Serbia', 'Slovakia', 'Slovenia', 'Spain', 'Sweden', 'Switzerland', 'United Kingdom', 'Monaco']
    Africa = ['Benin', 'Cameroon', 'Central African Republic', 'Democratic Republic of Congo', 'Ghana', 'Liberia', 'Morocco', 'Nigeria', 'South Africa', 'Sudan']
    Asia = ['Cyprus', 'India', 'Israel', 'Japan', 'Lebanon', 'Philippines', 'Qatar', 'Russia', 'Saudi Arabia', 'Singapore', 'South Korea', 'Taiwan', 'Thailand', 'Turkey', 'United Arab Emirates', 'Indonesia', 'Iran']
    Oceania = ['Australia', 'New Zealand', 'New Caledonia']
    if x in NorthAmerica:
        return 'North America'
    elif x in SouthAmerica:
        return 'South America'
    elif x in Europe:
        return 'Europe'
    elif x in Africa:
        return 'Africa'
    elif x in Asia:
        return 'Asia'
    else:
        return 'Oceania'
continent = []
for country in monkeypox_countr['location']:
    y = get_continent_country(country)
    continent.append(y)
monkeypox_countr['Continent'] = pd.Series(continent)
monkeypox_cont = monkeypox_countr[['Continent', 'location', 'date', 'new_cases', 'new_cases_smoothed', 'total_cases', 'new_cases_per_million', 'total_cases_per_million', 'new_cases_smoothed_per_million', 'new_deaths', 'new_deaths_smoothed', 'total_deaths', 'new_deaths_per_million', 'total_deaths_per_million', 'new_deaths_smoothed_per_million']].copy()

total_cases_continent = monkeypox_cont.set_index('Continent').groupby(level=0)['new_cases'].agg(np.sum).sort_values(ascending=False)
total_cases_country = monkeypox_cont.set_index('location').groupby(level=0)['new_cases'].agg(np.sum).sort_values(ascending=False)[:20]

Europe = monkeypox_cont[monkeypox_cont['Continent'] == 'Europe'].copy()
Europe['new_cases_%'] = Europe['new_cases'] / np.sum(Europe['new_cases'])
Europe = Europe.set_index('location').groupby(level=0)['new_cases_%'].agg(np.sum).sort_values(ascending=False)[:5]
NorthAmerica = monkeypox_cont[monkeypox_cont['Continent'] == 'North America'].copy()
NorthAmerica['new_cases_%'] = NorthAmerica['new_cases'] / np.sum(NorthAmerica['new_cases'])
NorthAmerica = NorthAmerica.set_index('location').groupby(level=0)['new_cases_%'].agg(np.sum).sort_values(ascending=False)[:3]

#PRINT GRAPH EUROPE

def graph_1():
    # make figure and assign axis objects
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 5))
    fig.subplots_adjust(wspace=0)
    plt.style.use('seaborn-colorblind')

    # pie chart parameters
    cases = list(total_cases_continent.values)
    labels = list(total_cases_continent.index)
    explode = [0.1, 0, 0,0,0,0]
    # rotate so that first wedge is split by the x-axis
    angle = -270 * cases[0]
    wedges, *_ = ax1.pie(cases, autopct='%1.1f%%', startangle=angle,
                         labels=labels, explode=explode, rotatelabels=True)

    # bar chart parameters Europe
    age_ratios = list(Europe.values)
    age_labels = list(Europe.index)
    bottom = .82
    width = .2

    # Adding from the top matches the legend. Europe
    for j, (height, label) in enumerate(reversed([*zip(age_ratios, age_labels)])):
        bottom -= height
        bc = ax2.bar(0, height, width, bottom=bottom, color='C0', label=label,
                     alpha=0.1 + 0.15 * j)
        ax2.bar_label(bc, labels=[f"{height:.1%}"], label_type='center')
    
    ax1.set_title('Total cases in the World')
    ax2.set_title('% Cases in Europe: Top 5')
    ax2.legend()
    ax2.axis('off')
    ax2.set_xlim(- 1.5 * width, 2.5 * width)

    # use ConnectionPatch to draw lines between the two plots:Europe
    theta1, theta2 = wedges[0].theta1, wedges[0].theta2
    center, r = wedges[0].center, wedges[0].r
    bar_height = sum(age_ratios)

    # draw top connecting line
    x = r * np.cos(np.pi / 180 * theta2) + center[0]
    y = r * np.sin(np.pi / 180 * theta2) + center[1]
    con = ConnectionPatch(xyA=(-width / 2, bar_height), coordsA=ax2.transData,
                          xyB=(x, y), coordsB=ax1.transData)
    con.set_color([0, 0, 0])
    con.set_linewidth(4)
    ax2.add_artist(con)

    # draw bottom connecting line
    x = r * np.cos(np.pi / 180 * theta1) + center[0]
    y = r * np.sin(np.pi / 180 * theta1) + center[1]
    con = ConnectionPatch(xyA=(-width / 2, 0), coordsA=ax2.transData,
                      xyB=(x, y), coordsB=ax1.transData)
    con.set_color([0, 0, 0])
    ax2.add_artist(con)
    con.set_linewidth(4)

    return plt.show()
graph_1()

def graph_2():
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 5))
    fig.subplots_adjust(wspace=0)
    plt.style.use('seaborn-colorblind')
    cases = list(total_cases_continent.values)
    labels = list(total_cases_continent.index)
    explode = [0, 0.1, 0, 0, 0, 0]
    angle = -88 * cases[0]
    wedges, labels, pct_texts, *_ = ax1.pie(cases, autopct='%1.1f%%', startangle=angle, labels=labels, explode=explode, rotatelabels=True, labeldistance=1)
    for label, pct_text in zip(labels, pct_texts):
        pct_text.set_rotation(label.get_rotation())
    north_ratios = list(NorthAmerica.values)
    north_labels = list(NorthAmerica.index)
    bottom = 1
    width = 0.2
    for q, (height, label) in enumerate(reversed([*zip(north_ratios, north_labels)])):
        bottom -= height
        bc = ax2.bar(0, height, width, bottom=bottom, color='C1', label=label, alpha=0.1 + 0.25 * q)
        ax2.bar_label(bc, labels=[f'{height:.1%}'], label_type='center')
    ax2.set_title('% Cases in North America: Top 3')
    ax2.legend()
    ax2.axis('off')
    ax2.set_xlim(-1.5 * width, 2.5 * width)
    theta1, theta2 = (wedges[1].theta1, wedges[1].theta2)
    center, r = (wedges[1].center, wedges[1].r)
    bar_height = sum(north_ratios)
    x = r * np.cos(np.pi / 180 * theta2) + center[0]
    y = r * np.sin(np.pi / 180 * theta2) + center[1]
    con = ConnectionPatch(xyA=(-width / 2, bar_height), coordsA=ax2.transData, xyB=(x, y), coordsB=ax1.transData)
    con.set_color([0, 0, 0])
    con.set_linewidth(4)
    ax2.add_artist(con)
    x = r * np.cos(np.pi / 180 * theta1) + center[0]
    y = r * np.sin(np.pi / 180 * theta1) + center[1]
    con = ConnectionPatch(xyA=(-width / 2, 0), coordsA=ax2.transData, xyB=(x, y), coordsB=ax1.transData)
    con.set_color([0, 0, 0])
    ax2.add_artist(con)
    con.set_linewidth(4)
    return plt.show()
graph_2()