码迷,mamicode.com
首页 > 其他好文 > 详细

matplotlib双y周实例e

时间:2019-12-19 09:32:50      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:画图   png   大小   for   win   nump   print   off   pyplot   

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pymysql
import warnings
import random

warnings.filterwarnings("ignore")


def read_db(path):
close = pd.read_csv(path)
info = close[:-6]
zhi_list = []
for x in info[‘qx_zu‘][20:]:
x_index = info[info[‘qx_zu‘] == x].index
all_shuju = info[x_index[0] - 20:x_index[0] - 1]
zhi = np.mean(all_shuju[‘qx_zu‘]) - 2 * (np.std(all_shuju[‘qx_zu‘]))
info[21:][‘cha‘] = zhi
zhi_list.append(zhi)
info[‘cha‘] = 0
info[‘cha‘][20:] = np.array(zhi_list)

info[‘jieguo‘] = info[‘qx_zu‘] < info[‘cha‘]
info[‘jieguo‘][:20] = 0
x_true_index = info[info[‘jieguo‘] == True].index
x_false_index = info[info[‘jieguo‘] == False].index
x_small = info.loc[x_true_index]
x_small[‘jieguo‘] = x_small[‘close‘]
# x_small[‘jieguo‘] = 1
x_big = info.loc[x_false_index]
x_big[‘jieguo‘] = 0
info_all = x_big.append(x_small)
info_all = info_all.sort_index()
# print(list(info_all[‘jieguo‘]))
# info_all.to_csv(‘info_all.csv‘)
info_all[‘date‘] = pd.to_datetime(info_all[‘date‘], format=‘%Y/%m/%d‘)
print(info_all[‘date‘])
return info_all


def show_info(info):
"""
画图展示
:param info:
:return:
"""

plt.rcParams[‘font.sans-serif‘] = [‘SimHei‘] # 设置全局显示汉子
plt.tick_params(axis=‘x‘, labelsize=10) # 设置x轴标签大小
plt.plot(info[‘date‘], info[‘close‘], ‘black‘)
plt.legend(loc=4)

plt.xlabel(‘时间‘) # 设置x轴标题

plt.ylabel(‘沪深300收盘价‘, color=‘black‘) # 设置Y1轴标题
# plt.xticks(rotation=110) # 设置时间标签显示格式
plt.twinx() # 添加一条Y轴,
plt.axis(‘off‘) #不显示刻度
plt.ylabel(‘预警信号‘, color=‘r‘) # 设置Y2轴标题

plt.bar(info[‘date‘], info[‘jieguo‘], label=‘预警信号‘, color=‘r‘)

plt.legend(loc=‘upper right‘) # 右上
# 保存图片
# plt.savefig(‘image/{}.png‘.format(‘沪深300收盘价‘))
plt.show()


if __name__ == ‘__main__‘:
show_info(info=read_db(‘h300_close.csv‘))

matplotlib双y周实例e

标签:画图   png   大小   for   win   nump   print   off   pyplot   

原文地址:https://www.cnblogs.com/guofeng-1016/p/12065811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!