Sunday, 15 December 2019

matplotlib grouped, stacked bar chart

from matplotlib import pyplot as plt
import numpy as np

ages = range(25,36)
salary = [38496, 42000, 46752, 49320, 53200, 56000, 62316, 64928, 67317, 68748, 73752]
py_salary = [45372, 48876, 53850, 57287, 63016, 65998, 70003, 70000, 71496, 75370, 83640]
js_salary = [37810, 43515, 46823, 49293, 53437, 56373, 62375, 66674, 68745, 68746, 74583]

index = np.arange(len(ages))
width = 0.25

plt.bar(index-width, salary, width=width, color='#444444', label = 'all salaries')
plt.bar(index, py_salary, width=width, color='#008fd5', label = 'python')
plt.bar(index+width, js_salary, width=width, color='#e5ae38', label = 'javascript')

plt.xticks(index, ages)

plt.legend()
plt.title('median salary by age')
plt.xlabel('ages')
plt.ylabel('usd')

from matplotlib import pyplot as plt
import numpy as np

ages = range(25,36)
salary = [38496, 42000, 46752, 49320, 53200, 56000, 62316, 64928, 67317, 68748, 73752]
py_salary = [45372, 48876, 53850, 57287, 63016, 65998, 70003, 70000, 71496, 75370, 83640]
js_salary = [37810, 43515, 46823, 49293, 53437, 56373, 62375, 66674, 68745, 68746, 74583]

stacked_salary = np.add(salary, py_salary)
print stacked_salary, len(stacked_salary)

index = np.arange(len(ages))
width = 0.5

plt.bar(index, salary, width=width, color='#444444', label = 'all salaries')
plt.bar(index, py_salary, bottom=salary, width=width, color='#008fd5', label = 'python')
plt.bar(index, js_salary, bottom=stacked_salary, width=width, color='#e5ae38', label = 'javascript')

plt.xticks(index, ages)

plt.legend()
plt.title('median salary by age')
plt.xlabel('ages')
plt.ylabel('usd')

[ 83868  90876 100602 106607 116216 121998 132319 134928 138813 144118
 157392] 11
from matplotlib import pyplot as plt
import numpy as np

ages = range(25,36)
salary = [38496, 42000, 46752, 49320, 53200, 56000, 62316, 64928, 67317, 68748, 73752]
py_salary = [45372, 48876, 53850, 57287, 63016, 65998, 70003, 70000, 71496, 75370, 83640]
js_salary = [37810, 43515, 46823, 49293, 53437, 56373, 62375, 66674, 68745, 68746, 74583]

index = np.arange(len(ages))
height = 0.25

plt.barh(index-height, salary, height=height, color='#444444', label = 'all salaries')
plt.barh(index, py_salary, height=height, color='#008fd5', label = 'python')
plt.barh(index+height, js_salary, height=height, color='#e5ae38', label = 'javascript')

plt.yticks(index, ages)

plt.legend()
plt.title('median salary by age')
plt.ylabel('ages')
plt.xlabel('usd')

reference:
https://www.youtube.com/watch?v=nKxLfUrkLE8&list=PL-osiE80TeTvipOqomVEeZ1HRrcEvtZB_&index=2
https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/barchart.html#sphx-glr-gallery-lines-bars-and-markers-barchart-py
https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/bar_stacked.html#sphx-glr-gallery-lines-bars-and-markers-bar-stacked-py

No comments:

Post a Comment