Matplotlib Time Series(matplotli
2019-04-30 本文已影响0人
呢嘻嘻嘻嘻嘻
Code from matplotlib doc
根据需求添加了内容,下次随用随取
'''
Time Series plt show
samples format: dataframe
data['date', 'CBCFI']
e.g.: 2014-4-28, 715.59
'''
import matplotlib.dates as mdates
from datetime import datetime
import matplotlib.cbook as cbook
import matplotlib.ticker as ticker
fig, axes = plt.subplots( figsize=(12, 6))
N = len(data)
ind = np.arange(N) #数据索引
'''
font
'''
font = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 14,}
date = data.date.astype('O')
test_date = data[(data.date == '2019-1-14')].index.tolist()[0]
val_date = data[(data.date == '2018-7-26')].index.tolist()[0]
def format_date(x, pos=None):
thisind = np.clip(int(x + 0.5), 0, N - 1)
return date[thisind].strftime('%Y-%m-%d') #转换时间格式
ax = axes
ax.plot(ind, data.CBCFI, '-' ,linewidth=1)
ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
ax.set_title("CBCFI History Values",font1)
fig.autofmt_xdate() #斜着显示时间戳
'''
设置刻度值的font
'''
plt.tick_params(labelsize=12)
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]
'''
插入竖线
'''
axes.axvline(x=val_date, color='g', linewidth=1,linestyle = '-.')
axes.axvline(x=test_date, color='r', linewidth=1,linestyle = ':')
'''
设置横纵坐标的font
'''
plt.ylabel('Index Value', font)
plt.xlabel('Time',font)
plt.autoscale(enable=True, axis='both')
'''
设置图例font
'''
#plt.legend(prop=font)
plt.savefig('F:DataFolder/coal/CBCFI.jpg')
plt.show()
#显示中文
import matplotlib.pyplot as plt
plt.xlabel("x轴")
plt.ylabel("y轴", fontproperties="SimSun") # 步骤一 (宋体)
plt.title("标题", fontproperties="SimHei") # (黑体)
plt.show()
显示效果