In [ ]:
#Matplotlib tries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc., with just a few lines of code.
In [3]:
from matplotlib import pyplot as plt
In [7]:
#values of x axis [ages from 25 to 35]
dev_x = [25,26,27,28,29,30,31,32,33,34,35]
#values of y axis [ Corresponding salaries ]
dev_y =[38496, 42000, 46752, 49320, 53200,
       56000, 62316, 64928, 67317, 68748, 73752]
In [8]:
plt.plot(dev_x,dev_y)
#plt.show()
Out[8]:
[<matplotlib.lines.Line2D at 0x7fe75f3937f0>]
In [12]:
#adding details
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')
plt.title('Median Salary (USD) by age')
plt.plot(dev_x,dev_y)
plt.show()
In [13]:
# Median Python Developer Salaries by Age
py_dev_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
In [18]:
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')
plt.title('Median Salary (USD) by age')
plt.plot(dev_x,dev_y)
plt.plot(py_dev_x,py_dev_y)
#adding legend, order is based on developers are added first and the python, but this is error prone as you need to know which you have added first 
plt.legend(['All Devs', 'Python'])
plt.show()
In [19]:
#Better way to add legend, passing legend while plotting
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')
plt.title('Median Salary (USD) by age')

plt.plot(dev_x,dev_y, label='All Devs')
plt.plot(py_dev_x,py_dev_y, label='Python')

plt.legend()
plt.show()
In [26]:
#adding color, line style and marker
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')
plt.title('Median Salary (USD) by age')

plt.plot(dev_x,dev_y, color='k', linestyle='--', marker='*', label='All Devs') #k is for black color
plt.plot(py_dev_x,py_dev_y, color='b', marker='^', label='Python') #b is for blue color

plt.legend()
plt.show()
#link for more option of formatting : http://bit.ly/Matplotlib-Fmt-Str
In [27]:
# Median JavaScript Developer Salaries by Age
js_dev_y = [37810, 43515, 46823, 49293, 53437,
            56373, 62375, 66674, 68745, 68746, 74583]
In [51]:
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')
plt.title('Median Salary (USD) by age')

#using hexacolor and adding linewidth
plt.plot(py_dev_x,py_dev_y, color='#5a7d9a', marker='*', linewidth=2, label='Python')
plt.plot(py_dev_x,js_dev_y, color='#adad3b', marker='^', linewidth=2, label='JavaScript')
plt.plot(dev_x,dev_y, color='#444444', marker='o', linestyle='--', label='All Devs')

plt.legend()
#adding grid
plt.grid()

#for better padding
plt.tight_layout()

#adding style
plt.style.use('seaborn-ticks')

#saving it as png
plt.savefig('plot.png')

plt.show()
In [35]:
#available style with pyplot
print(plt.style.available)
#plt.xkcd() #comic style
['seaborn-bright', 'bmh', 'dark_background', 'seaborn-pastel', 'seaborn-ticks', 'seaborn-deep', 'seaborn-whitegrid', 'grayscale', 'Solarize_Light2', 'fast', 'seaborn-white', 'seaborn-dark-palette', 'seaborn-poster', 'seaborn', 'seaborn-paper', 'seaborn-darkgrid', 'seaborn-muted', 'seaborn-colorblind', 'ggplot', 'tableau-colorblind10', 'seaborn-talk', '_classic_test', 'seaborn-notebook', 'classic', 'fivethirtyeight', 'seaborn-dark']