Mathplotlib

Mathplotlib is a Python plotting library.


In [232]:
import numpy as np
import matplotlib.pyplot as plt

Pie


In [256]:
plt.pie(
    (10,20,30,13,17), # values 
    colors = ('#8ff0df','#56dee1','#4291c1','#116994','#12315c'),
    labels = ('10%','20%','30%','13%','17%'),
    explode= (0,0.05,0,0,0) # offsetting a slice
)
plt.title('Pie Chart')
plt.show()


Plots

The plot() supports an argument list for multiple lines:


In [233]:
x = np.arange(0.,5.,0.1)
y = x[::-1] # reverse
# plot x versus y axis
plt.plot(
    x,     y,       'g-',     # green, solid
    x,     y**2,    'r--',    # red, dashed
    x+0.8, y,       'b-.',    # blue, dash-dot 
    x,     y**2.1,  'y.',     # yellow, dotted
)
plt.show()


Call the plot() function multiple times with labeled line configuration:


In [234]:
# RGBA color tuple
plt.plot(x,y,color=(1,0.4,0,0.75),label='orange',linewidth=.5)
# HEX color
plt.plot(x,y**2,color='#00800066',label='green',linestyle='dotted')
plt.legend() # display legend
plt.title('Title')
plt.xlabel('x label')
plt.ylabel('y label')
plt.text(1.1,4.9,'abc',fontsize=14,color='gray',style='italic')
plt.text(0.7,20.5, r'$E=mc^2$', fontsize=10) # math equation in text
plt.show()


Histograms


In [235]:
s = np.random.randn(1000)
s = 0.9 * np.random.randn(1000)
t = 2 + np.random.randn(1000)


plt.hist([s,r,t])
plt.xlabel("value")
plt.ylabel("frequency")
plt.show()



In [236]:
plt.hist(
    [s,r,t],
    stacked=True,
    width=0.75,
    color=['#8595a5','#bcccdc','#e8cfca'],
    alpha=0.75,
    label=['abc','def','ghi']
)
plt.legend(loc='upper left')
plt.show()


Timeseries


In [237]:
import datetime as dt
# the next 24 days
now = dt.datetime.now()
dates = np.array([now + dt.timedelta(days=i) for i in range(24)])
values = np.random.randint(100, size=dates.shape) # random values per day

plt.plot(dates,values,alpha=0.75,marker='.')
plt.gcf().autofmt_xdate() # rotate x-axis
plt.show()