I am trying to generate population plot using code below. I re used some of code I found. However, I dont know I can optimise legends according to the data range I have. I mean so I should have nice plot which is squished because of wrong x.axis limit.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
age = np.array(["0-9", "10-19", "20-29", "30-39", "40-49", "50-59", "60-69", "70-79", "80-89", '90-99',
"100-109", "110-119", "120-129", "130-139", "140-150", ">150"])
m = np.array([811, 34598, 356160, 381160, 243330, 206113, 128549, 60722, 8757, 1029, 1033, 891, 1803, 62, 92, 764])
f = np.array(
[612, 101187, 904717, 841066, 503661, 421678, 248888, 95928, 10289, 1444, 1360, 1377, 1699, 119, 173, 1655])
x = np.arange(age.size)
tick_lab = ['3M', '2M', '1M', '1M', '2M', '3M']
tick_val = [-3000000, -2000000, -1000000, 1000000, 2000000, 3000000]
plt.figure(figsize=(16, 8), dpi=80)
def plot_pyramid():
plt.barh(x, -m, alpha=.75, height=.75, left=-shift, align='center' , color="deepskyblue")
plt.barh(x, f, alpha=.75, height=.75, left = shift, align='center', color="pink")
plt.yticks([])
plt.xticks(tick_val, tick_lab)
plt.grid(b=False)
plt.title("Population Pyramid")
for i, j in enumerate(age):
if i == 0 or i==1:
plt.text(-150000, x[i] - 0.2, j, fontsize=14)
else:
plt.text(-230000, x[i] - 0.2, j, fontsize=14)
if __name__ == '__main__':
plot_pyramid()
Any help will be appreciated
Thanks in advance