import matplotlib.pyplot as plt
import pandas as pd
plt.rc('figure', figsize=(10, 8))
plt.style.use('seaborn-white')
data = pd.read_csv(r'examples/age_data.csv')
data.head()
Age | All_Devs | Python | JavaScript | |
---|---|---|---|---|
0 | 18 | 17784 | 20046 | 16446 |
1 | 19 | 16500 | 17100 | 16791 |
2 | 20 | 18012 | 20000 | 18942 |
3 | 21 | 20628 | 24744 | 21780 |
4 | 22 | 25206 | 30500 | 25704 |
ages = data['Age']
dev_salaries = data['All_Devs']
py_salaries = data['Python']
js_salaries = data['JavaScript']
# fill entire area under python plotted area
overall_median = 57287
plt.plot(ages, dev_salaries, color='#444444', linestyle='--', label='All Devs')
plt.plot(ages, py_salaries, label='Python')
# plt.fill_between(ages, py_salaries, alpha=0.25)
# fill between the overall median
# plt.fill_between(ages, py_salaries, alpha=0.25, y2=overall_median)
# filling only when a condition is met
# fill with green when we are above the median salary
plt.fill_between(ages, py_salaries, alpha=0.25, y2=overall_median, color='green',
where=(py_salaries > overall_median), interpolate=True)
# fill with red when we are below the median salary
plt.fill_between(ages, py_salaries, alpha=0.25, y2=overall_median, color='red',
where=(py_salaries <= overall_median), interpolate=True)
# interpolate - make sure certain x-intersections don't get clipped
plt.legend()
plt.title('Median Salary (USD) by Age')
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')
plt.show()
# fill area between two different plots
plt.plot(ages, dev_salaries, color='#444444', linestyle='--', label='All Devs')
plt.plot(ages, py_salaries, label='Python')
# filling only when a condition is met
# fill with green when we are above the median salary
plt.fill_between(ages, py_salaries, alpha=0.25, y2=dev_salaries, color='green',
where=(py_salaries > dev_salaries), interpolate=True,
label='Above AVG')
# fill with red when we are below the median salary
plt.fill_between(ages, py_salaries, alpha=0.25, y2=dev_salaries, color='red',
where=(py_salaries <= dev_salaries), interpolate=True,
label='Below AVG')
# interpolate - make sure certain x-intersections don't get clipped
plt.legend()
plt.title('Median Salary (USD) by Age')
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')
plt.show()