一、pyplot
(1)绘图区域
plt.subplot(nrows, ncols, plot_number)
e.g.
import numpy as np
import matplotlib.pyplot as plt
def fun(x):
return np.exp(-x) * np.cos(2*np.pi*x)
a=np.arange(0.0, 5.0, 0.02)
plt.subplot(211)
plt.plot(a, fun(a))
plt.subplot(2,1,2)
plt.plot(a, np.cos(2*np.pi*a), 'r--')
plt.show
(2)plot函数
plt.plot(x, y, format_string, **kwargs)
format_string由
颜色字符,'r', '#008000'
;
风格字符'-','--'
;
标记字符组成'.','o','v'
。
e.g.
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(10)
plt.plot(a, a*1.5, 'go-', a, a*2.5, 'rx', a, a*3.5, '', a, a*4.5, 'b-.')
plt.show()
(3)文本显示
plt.xlabel()
plt.ylabel()
plt.title()
plt.text()
plt.annotate()
e.g.
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(0.0, 5.0, 0.02)
plt.plot(a, np.cos(2*np.pi*a), 'r--')
plt.xlabel('time', fontsize=15, color='green')
plt.ylabel('frequence', fontsize=15)
plt.title(r'$y=cos(2\pi x)$', fontsize=25)
plt.text(2, 1, r'$\mu=100$', fontsize=15)
plt.axis([-1,6,-2,2])
plt.grid(True)
plt.show()
(4)子绘图区域
plt.subplot2grid(GridSpec, CurSpec, colspan=1, rowspan=1)
e.g.
plt.subplot2grid((3,3), (1,0), colspan=2)
GridSpec类
gs = gridspec.GridSpec(3,3)
ax1 = plt.subplot(gs[0, :])
二、金融学图表
http://blog.csdn.net/u014281392/article/details/73611624
http://www.cnblogs.com/hhh5460/p/5120079.html
matplotlib.finance 中的quotes_historical_yahoo_ohlc中的yahoo源已失效,考虑使用tushare获取数据
# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
import tushare as ts
import numpy as np
import pandas as pd
cmb = ts.get_k_data('600036', '2017-06-01')
print cmb.info()
print cmb[:3]
<class 'pandas.core.frame.DataFrame'>
Int64Index: 168 entries, 97 to 264
Data columns (total 7 columns):
date 168 non-null object
open 168 non-null float64
close 168 non-null float64
high 168 non-null float64
low 168 non-null float64
volume 168 non-null float64
code 168 non-null object
dtypes: float64(5), object(2)
memory usage: 10.5+ KB
None
date open close high low volume code
97 2017-06-01 21.595 21.847 21.905 21.499 780922.0 600036
98 2017-06-02 21.953 21.431 22.002 21.344 510194.0 600036
99 2017-06-05 21.209 21.257 21.441 20.889 650265.0 600036
tussore的数据类型是DataFrame,将其转换mpf.candlestick_ochl()可处理的浮点数格式mat_cmb
[[736481.0 21.595 21.847 21.905 21.499 780922.0 '600036']
[736482.0 21.953 21.431 22.002 21.344 510194.0 '600036']
[736485.0 21.209 21.257 21.441 20.889 650265.0 '600036']]
fig, ax = plt.subplots(figsize=(15, 5))
fig.subplots_adjust(bottom=0.2)
mpf.candlestick_ochl(ax, mat_cmb, width=0.6, colorup='b', colordown='r')
plt.grid(True)
ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=30)
plt.show()
colordown:收盘价小于开盘价时的矩形颜色
colorup:收盘价大于等于开盘价时的矩形颜色