Created
November 22, 2020 20:29
-
-
Save anil-kk/322b4950b47a82f799a94cf64266afe8 to your computer and use it in GitHub Desktop.
Horizontal bar with label for each bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output