image_output_1.png
import matplotlib.pyplot as plt

import matplotlib.ticker as mtick

import pandas as pd

df = pd.read_csv('../input/chinas-population-by-gender-and-urbanrural/Chinas Population En.csv')

df.columns = ['year', 'total', 'male', 'female', 'urban', 'rural']
df.sort_values(by='year', ignore_index=True, inplace=True)

tmp_mask = df['total'] - df['male'] - df['female'] != 0
df[tmp_mask]

df.loc[tmp_mask, 'total'] = df.loc[tmp_mask, 'male'] + df.loc[tmp_mask, 'female']

fig, ax1 = plt.subplots(figsize=(12, 5))

plt.title('The difference between the genders in total and percentage terms', fontweight='bold', fontsize = 12)
ax1.set_xlabel('year', fontsize = 10,fontweight='bold')
ax1.set_ylabel('total',fontweight='bold', fontsize = 10, color = 'green')
plt.plot(df['year'], df['male'] - df['female'], linewidth=3,label= 'total', color = 'green')
ax1.tick_params(axis='y')

ax2 = ax1.twinx() 
ax2.set_ylabel('percent', fontweight='bold', fontsize = 10)
plt.plot(df['year'], (df['male'] - df['female'])/df['total']*100, linewidth=3, color = 'black', label= 'percent')
ax2.tick_params(axis='y')
ax2.yaxis.set_major_formatter(mtick.PercentFormatter())

fig.tight_layout() 

fig, ax1 = plt.subplots(figsize=(12, 5))
plt.title('Changing of population growth', fontweight='bold', fontsize=12)
ax1.set_xlabel('year', fontsize=10, fontweight='bold')
ax1.set_ylabel('total number', fontweight='bold', fontsize=10, color='green')
plt.plot(df['year'], df['total'], linewidth=3, label='total', color='green')
ax1.tick_params(axis='y')
ax2 = ax1.twinx()
ax2.set_ylabel('total growth', fontweight='bold', fontsize=10)
plt.plot(df['year'], df['total'] - df['total'].shift(), linewidth=3, color='black', label='percent')
ax2.tick_params(axis='y')
plt.axhline(y=0, color='red', linestyle='--')
fig.tight_layout()