In [11]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
In [7]:
df1 = pd.read_csv('df1.csv', index_col = 0)
df1.head()
Out[7]:
In [8]:
df2 = pd.read_csv('df2.csv')
df2.head()
Out[8]:
In [12]:
df1['A'].hist(bins= 30)
Out[12]:
In [13]:
df1['A'].plot(kind ='hist', bins = 30)
Out[13]:
In [15]:
df1['A'].plot.hist(bins = 30)
Out[15]:
In [20]:
df2
Out[20]:
In [45]:
df2.plot.area(alpha = 0.4)
Out[45]:
In [21]:
# index will be the x-axis
df2.plot.bar()
Out[21]:
In [22]:
df2.plot.bar(stacked = True)
Out[22]:
In [23]:
df1['A'].plot.hist(bins = 50)
Out[23]:
In [27]:
df1.plot.line(x = df1.index, y = 'B', figsize=(12,3), lw = 0.5)
Out[27]:
In [30]:
df1.plot.scatter(x='A', y='B', c = 'C', cmap= 'coolwarm')
Out[30]:
In [34]:
df1.plot.scatter(x='A', y='B', s = df1['C'] * 10) # s = size
Out[34]:
In [35]:
df2.plot.box()
Out[35]:
In [38]:
df = pd.DataFrame(np.random.randn(1000,2), columns=['a', 'b'])
df.head(10)
Out[38]:
In [39]:
df.plot.hexbin(x = 'a', y = 'b')
Out[39]:
In [41]:
df.plot.hexbin(x = 'a', y = 'b', gridsize = 25, cmap = 'coolwarm')
Out[41]:
In [42]:
df2['a'].plot.kde()
Out[42]:
In [43]:
df2['a'].plot.density()
Out[43]:
In [44]:
df2.plot.kde()
Out[44]:
In [46]:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
In [47]:
mcdon = pd.read_csv('mcdonalds.csv', index_col = 'Date', parse_dates = True)
mcdon.head()
Out[47]:
In [49]:
# Note the Adj. Volume is way scaled-up than Adj. Close, hence the plot is not so nice
mcdon.plot()
Out[49]:
In [50]:
mcdon['Adj. Close'].plot()
Out[50]:
In [52]:
mcdon['Adj. Volume'].plot(figsize=(12,4))
Out[52]:
In [55]:
mcdon['Adj. Close'].plot(xlim = ['2007-01-01', '2009-01-01']) #xlimit
Out[55]:
In [59]:
#xlimit and ylimit , [list] or (tuple) both accepted
mcdon['Adj. Close'].plot(xlim = ['2007-01-01', '2009-01-01'], ylim = (20,50))
Out[59]:
In [60]:
mcdon['Adj. Close'].plot(xlim = ['2007-01-01', '2009-01-01'], ylim = (20,50), ls = '--', c='red')
Out[60]:
In [61]:
import matplotlib.dates as pltdates
In [62]:
mcdon['Adj. Close'].plot(xlim = ['2007-01-01', '2009-01-01'], ylim = (20,50))
Out[62]:
In [63]:
idx = mcdon.index
idx
Out[63]:
In [64]:
idx = mcdon.loc['2007-01-01':'2007-05-01'].index
idx
Out[64]:
In [65]:
stock = mcdon.loc['2007-01-01':'2007-05-01']['Adj. Close']
stock
Out[65]:
In [72]:
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
ax.yaxis.grid(True)
ax.xaxis.grid(True)
fig.autofmt_xdate() # automatically format the x-date axis
plt.tight_layout()
In [81]:
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
ax.xaxis.grid(True)
ax.yaxis.grid(True)
# http://strftime.org/
ax.xaxis.set_major_locator(pltdates.MonthLocator())
ax.xaxis.set_major_formatter(pltdates.DateFormatter('\n\n%b-%Y'))
ax.xaxis.set_minor_locator(pltdates.WeekdayLocator(byweekday=0))
ax.xaxis.set_minor_formatter(pltdates.DateFormatter('%d')) # try %a
fig.autofmt_xdate() # automatically format the x-date axis
plt.tight_layout()
In [ ]: