Skip to content

Instantly share code, notes, and snippets.

@anil-kk
Created November 22, 2020 20:29
Show Gist options
  • Save anil-kk/322b4950b47a82f799a94cf64266afe8 to your computer and use it in GitHub Desktop.
Save anil-kk/322b4950b47a82f799a94cf64266afe8 to your computer and use it in GitHub Desktop.
Horizontal bar with label for each bar
import pandas as pd
import numpy as np
# %matplotlib inline
import matplotlib.pyplot as plt
import matplotlib as mpl
#'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/Canada.xlsx'
df = pd.read_excel('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/Canada.xlsx',
sheet_name='Canada by Citizenship',
skiprows=range(20),
skipfooter=2)
df.drop(['Type', 'Coverage','AREA','REG', 'DEV'], axis=1, inplace=True)
df.rename(columns={'OdName': 'Country', 'AreaName': 'Continent','RegName': 'Region'}, inplace=True)
df['Total']=df.sum(axis=1)
df=df.set_index('Country')
mpl.style.use(['ggplot'])
years = list(map(int, range(1980, 2014)))
df_top10 = pd.DataFrame(df.nlargest(15,'Total')['Total'].sort_values(ascending=True))
ax=df_top10.plot.barh(legend=False, color='crimson', edgecolor='LightCoral')
plt.xlabel('Number of Imigrants', color='black')
plt.ylabel('Year', color='black')
plt.title('Top 15 Immigrant Countries to Canada from 1980-2013 ', color='black')
plt.xticks(color='black')
plt.yticks(color='black')
for i, total in enumerate(df_top10['Total']):
ax.text(total + 1000, i, str(total), color='black', fontweight='bold', fontsize=6, ha='left', va='center')
plt.show()
@anil-kk
Copy link
Author

anil-kk commented Nov 22, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment