画折线
import matplotlib.pyplot as plt
plt.plot([1,2,3,4],[5,4,2,8])
plt.plot([1,2,3,4],[6,5,3,9],marker="o")
plt.show()
画点
plt.plot([1,2,3,4],[5,4,2,8],"o")
plt.show()
画柱状图
data=[20,50,66,89]
labels=['A','B','C','D']
plt.bar(np.arange(len(data)),data)
plt.xticks(np.arange(len(data)),labels)
plt.show()
画饼图
plt.pie([10,20,30,40],labels=["a","b","c","d"],autopct="%.2f%%",explode=[0.1,0,0.2,0])
plt.axis("equal")
plt.show()
画布模式,画多个图
fig=plt.figure()
plt1=fig.add_subplot(2,2,1)
plt1.plot([1,2,3,4],[5,6,7,8])
plt2=fig.add_subplot(2,2,2)
plt2.plot([2,3,4,5],[1,5,7,2])
plt3=fig.add_subplot(2,2,3)
plt3.plot([2,3,4,5],[2,5,4,2])
plt.show()
添加注释
plt.plot([1,2,3,4],[5,4,2,8],label="line1")
plt.plot([1,2,3,4],[6,5,3,9],marker="o",label="line2")
plt.xlabel("x label")
plt.ylabel("y label")
plt.title("title")
plt.legend()
plt.show()
绘制K线图
data数据如下,从excel读取,总共一个月的数据:
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
import matplotlib.ticker as ticker
def Format_func(x, pos):
return data.index[pos].strftime('%Y-%m-%d')
data['index'] = date2num(data.index.to_pydatetime())
>>> close high low open volume index
2020-04-22 19.29 19.34 18.80 18.96 29245502.0 737537.0
2020-04-23 18.89 19.50 18.86 19.47 35230658.0 737538.0
.........
quotes = data[['index','open','close','high','low']].values
>>> array([[7.37537e+05, 1.89600e+01, 1.92900e+01, 1.93400e+01, 1.88000e+01],
[7.37538e+05, 1.94700e+01, 1.88900e+01, 1.95000e+01, 1.88600e+01],...)
fig=plt.figure()
ax1=fig.add_subplot(1,1,1)
ax1.set_title(symbol)
ax1.set_ylabel('价格')
ax1.xaxis.set_major_formatter(ticker.FuncFormatter(Format_func))
ax1.grid(True)
fin.candlestick_ochl(ax1,quotes,colordown='g', colorup='r',width=0.2)
plt.xticks(rotation=30)
plt.show()
|