Matplotlib数据可视化

导入文件并画图
import os
import pandas as pd
os.chdir('e:\\')
ChinaBank=pd.read_csv('ChinaBank.csv',index_col='Date')
ChinaBank=ChinaBank.iloc[:,1:]
print(ChinaBank.head())
ChinaBank.index=pd.to_datetime(ChinaBank.index)
Close=ChinaBank.Close

import matplotlib.pyplot as plt
plt.plot(Close['2014'])
plt.show()

 

 

简单画图

import matplotlib.pyplot as plt
plt.plot([1,1,0,0,-1,0,1,1,-1])
plt.show()

 

 

更改坐标轴范围

import matplotlib.pyplot as plt
plt.plot([1,1,0,0,-1,0,1,1,-1])
plt.ylim(-1.5,1.5)  #Y轴改成从-1.5到1.5
plt.show()

 

 修改坐标轴标签

import matplotlib.pyplot as plt
plt.plot([1,1,0,0,-1,0,1,1,-1])
plt.ylim(-1.5,1.5)
plt.xticks(range(9),['2015-02-01','2015-02-02','2015-02-03','2015-02-04','2015-02-05','2015-02-06','2015-02-07','2015-02-08','2015-02-09'],rotation=45)
plt.show()

 

 添加标题

plt.title('中国银行2014年收盘价曲线',loc='right')

设定坐标轴标签

plt.xlabel('日期')
plt.ylabel('收盘价')

添加背景

plt.grid(True,axis='y')

添加Label

Open=ChinaBank.Open
plt.plot(Close['2014'],label='收盘价')
plt.plot(Open['2014'],label='开盘价')
plt.legend()

 修改线条类型

plt.plot(Close['2014'],label='收盘价',linestyle='solid')
plt.plot(Open['2014'],label='开盘价',ls='-.')

 

 修改线条的颜色

plt.plot(Close['2014'],c='r',label='收盘价')
plt.plot(Open['2014']c='b',ls='~~',label='开盘价')

 

 修改点的形状类型

plt.plot(Close['2015'],marker='o',label='收盘价')
plt.plot(Open['2014'],marker='*',label='开盘价')

 

 修改线条宽度

plt.plot(Close['2015'],'--rD',label='收盘价',linewidth=2)
plt.plot(Open['2014'],'--b>',label='开盘价',lw=10)

 

posted @ 2021-02-20 17:54  我的博客2021  阅读(142)  评论(0)    收藏  举报