In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from numpy.random import randn
from datetime import datetime
%matplotlib inline
In [ ]:
plt.rc('figure', figsize=(15,10))
font_option = {
'family': 'monospace',
'weight': 'bold',
'size': '12'
}
plt.rc('font', **font_option)
In [ ]:
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
plt.plot(randn(50).cumsum(), 'k--')
ax1.hist(randn(500), bins=20, color='k', alpha=0.3)
ax2.scatter(x=np.arange(30), y=np.arange(30) + 3 * randn(30))
In [ ]:
fig, axes = plt.subplots(2, 2)
axes[1,0].plot(randn(50).cumsum(), 'k--')
axes[0,0].hist(randn(500), bins=20, color='k', alpha=0.3)
axes[0,1].scatter(x=np.arange(30), y=np.arange(30) + 3 * randn(30))
In [ ]:
fig, axes = plt.subplots(nrows=2, ncols=2, sharex=True, sharey=True)
for i in range(2):
for j in range(2):
axes[i,j].hist(x=randn(500), bins=50, color='k', alpha=0.5)
plt.subplots_adjust(wspace=0, hspace=0)
In [ ]:
data = randn(10).cumsum()
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].plot(data, linestyle='--')
axes[0,1].plot(data, 'ko--')
axes[1,0].plot(data, color='k', marker='o', linestyle='--')
axes[1,1].plot(data, color='b', linestyle='dashed')
In [ ]:
fig = plt.figure(figsize=(15,10))
ax1 = fig.add_subplot(1,1,1)
ax1.plot(randn(1000).cumsum(), 'k', label='one')
ax1.plot(randn(1000).cumsum(), 'b--', label='two')
ax1.plot(randn(1000).cumsum(), 'r.', label='tree')
ax1.set_title("I'm title")
ax1.set_xticks([200, 400, 600, 800, 1000])
ax1.set_xticklabels(['l2', 'l4', 'l6', 'l8', 'l10'], rotation=30, fontsize='medium')
ax1.legend(loc='best')
In [ ]:
fig = plt.figure(figsize=(15, 10))
ax = fig.add_subplot(1,1,1)
ax.set_title('Annotation Test')
# 指定0列为行索引
data = pd.read_csv('./pydata-book/examples/spx.csv', index_col=0, parse_dates=True)
# data = pd.read_csv('./pydata-book/examples/spx.csv', parse_dates=True)
print(data[:5])
spx = data['SPX'] # Series
print(spx[:5])
spx.plot(ax=ax, style='k--')
# limit 2007 - 20110
ax.set_xlim(['1/1/2007', '1/1/2010'])
ax.set_ylim([600, 1800])
crisis_data = [
(datetime(2007, 10,11), 'AA'),
(datetime(2008, 3,12), 'BB'),
(datetime(2008, 9,15), 'CC')]
for date, label in crisis_data:
# print(spx.asof(date))
ax.annotate(label, xy=(date, spx.asof(date)+50),
xytext=(date, spx.asof(date)+200),
arrowprops=dict(facecolor='black'),
horizontalalignment='left', verticalalignment='top'
)
In [ ]:
data = np.random.randn(10).cumsum()
print(data)
s1 = pd.Series(data=data, index=np.arange(0, 100, 10))
s1.plot()
In [ ]:
data = np.random.randn(10, 4).cumsum(0)
print(data)
df1 = pd.DataFrame(data=data, columns=('A','B','C','D'), index=np.arange(0, 100, 10))
df1.plot()
In [ ]:
fig, axes = plt.subplots(2, 1)
s2 = pd.Series(
data=np.random.randn(16).cumsum(),
index=list('abcdefghijklmnop')
)
s2.plot(kind='bar', ax=axes[0], color='k', alpha=0.7)
s2.plot(kind='barh', ax=axes[1], color='k', alpha=0.7)
In [ ]:
df2 = pd.DataFrame(
data=np.random.rand(6,4),
index=['one','two', 'tree', 'four', 'five', 'six'],
columns=pd.Index(data=list('ABCD'), name='Genus')
)
print(df2)
df2.plot(kind='bar')
df2.plot(kind='barh', stacked=True, alpha=0.5)
In [ ]:
tips = pd.read_csv('./pydata-book/examples/tips.csv')
# print(type(tips.size))
# tips.size is the tips's size, is not column 'size', so using tips['size'] instead.
day_counts = pd.crosstab(tips.day, tips['size'])
day_counts
In [ ]:
tips['pct'] = tips['tip'] / tips['total_bill']
tips['pct'].hist(bins=50)