In [69]:
import numpy as np
import pandas as pd
%matplotlib inline
In [70]:
df1 = pd.read_csv('df1',index_col=0)
df2 = pd.read_csv('df2')
Matplotlib has style sheets you can use to make your plots look a little nicer. These style sheets include plot_bmh,plot_fivethirtyeight,plot_ggplot and more. They basically create a set of style rules that your plots follow. I recommend using them, they make all your plots have the same look and feel more professional. You can even create your own if you want your company's plots to all have the same look (it is a bit tedious to create on though).
Here is how to use them.
Before plt.style.use() your plots look like this:
In [71]:
df1['A'].hist()
Out[71]:
Call the style:
In [72]:
import matplotlib.pyplot as plt
plt.style.use('ggplot')
Now your plots look like this:
In [73]:
df1['A'].hist()
Out[73]:
In [74]:
plt.style.use('bmh')
df1['A'].hist()
Out[74]:
In [75]:
plt.style.use('dark_background')
df1['A'].hist()
Out[75]:
In [76]:
plt.style.use('fivethirtyeight')
df1['A'].hist()
Out[76]:
In [77]:
plt.style.use('ggplot')
Let's stick with the ggplot style and actually show you how to utilize pandas built-in plotting capabilities!
There are several plot types built-in to pandas, most of them statistical plots by nature:
You can also just call df.plot(kind='hist') or replace that kind argument with any of the key terms shown in the list above (e.g. 'box','barh', etc..)
In [78]:
df2.plot.area(alpha=0.4)
Out[78]:
In [79]:
df2.head()
Out[79]:
In [80]:
df2.plot.bar()
Out[80]:
In [81]:
df2.plot.bar(stacked=True)
Out[81]:
In [82]:
df1['A'].plot.hist(bins=50)
Out[82]:
In [83]:
df1.plot.line(x=df1.index,y='B',figsize=(12,3),lw=1)
Out[83]:
In [84]:
df1.plot.scatter(x='A',y='B')
Out[84]:
You can use c to color based off another column value Use cmap to indicate colormap to use. For all the colormaps, check out: http://matplotlib.org/users/colormaps.html
In [85]:
df1.plot.scatter(x='A',y='B',c='C',cmap='coolwarm')
Out[85]:
Or use s to indicate size based off another column. s parameter needs to be an array, not just the name of a column:
In [86]:
df1.plot.scatter(x='A',y='B',s=df1['C']*200)
Out[86]:
In [87]:
df2.plot.box() # Can also pass a by= argument for groupby
Out[87]:
In [88]:
df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])
df.plot.hexbin(x='a',y='b',gridsize=25,cmap='Oranges')
Out[88]:
In [89]:
df2['a'].plot.kde()
Out[89]:
In [90]:
df2.plot.density()
Out[90]:
That's it! Hopefully you can see why this method of plotting will be a lot easier to use than full-on matplotlib, it balances ease of use with control over the figure. A lot of the plot calls also accept additional arguments of their parent matplotlib plt. call.
Next we will learn about seaborn, which is a statistical visualization library designed to work with pandas dataframes well.
Before that though, we'll have a quick exercise for you!