学习记录绘制柱状图形显示A50ETF上涨与下跌的天数

”’学习记录:matplotlib 绘制柱状图形显示A50ETF上涨与下跌的天数同花顺下载的历史交易数据A50ETF 159601”’import pandasimport matplotlib.pyplot as pltimport datetime# 读取数据,同花顺下载的历史交易数据# A50ETF 159601shares: pandas.DataFrame = pandas.read_excel(r”.shares159601.xlsx”)# 获取涨跌幅天数up = pandas.DataFrame()down = pandas.DataFrame()flat = pandas.DataFrame()for i in range(shares.shape[0]): if shares.loc[i, ‘涨幅’] > 0: # concat # concat 可以沿着指定的轴将多个dataframe或者series拼接到一起 # 参数:ignore_index合并后重新设置Index索引值, # 通过axis设置合并的方向, # 0值是添加行,1值是添加列 up = pandas.concat([up, shares.loc[i]], axis=1, ignore_index=True) elif shares.loc[i, ‘涨幅’] == 0: flat = pandas.concat([flat, shares.loc[i]], axis=1, ignore_index=True) # print(shares.loc[i,’涨幅’]) elif shares.loc[i, ‘涨幅’] < 0: down = pandas.concat([down, shares.loc[i]], axis=1, ignore_index=True) # print(shares.loc[i, '涨幅'])# T 列行columns,index互换位置print("上涨天数:",up.T.shape[0])print('下跌天数:',down.T.shape[0])print('平盘天数:',flat.T.shape[0])# 绘制图形需要的数据sales = [up.T.shape[0], down.T.shape[0], flat.T.shape[0]]# 处理中文乱码plt.rcParams["font.sans-serif"] = ["SimHei"] # 设置字体# 该语句解决图像中的“-”负号的乱码问题plt.rcParams["axes.unicode_minus"] = False# 设置标题plt.title("A50ETF:159601,2021-11-08至2022-10-26")# 设置y轴标签plt.ylabel("天数")# 为每个条形图添加数值标签# enumerate() 函数它在遍历的同时还可以得到当前元素的索引位置。for x, y in enumerate(sales): plt.text(x, y 1, '%s天' %y, ha='center')# 绘制柱状图形plt.bar(x=['上涨天数', '下跌天数', '平盘天数'], height=sales, color=['r', 'g', 'b'])plt.show()# 2021-11-08至2022-10-26有多少天d1 = datetime.datetime.strptime('2021-11-08','%Y-%m-%d')d2 = datetime.datetime.strptime('2022-10-26','%Y-%m-%d')print("2021-11-08至2022-10-26有" str((d2-d1).days) "天")# 2021-11-08至2022-10-26有多少天print('A50ETF:159601,2021-11-08至2022-10-26有' str(shares.shape[0]) '个交易日')

未经允许不得转载:股市行情网 » 学习记录绘制柱状图形显示A50ETF上涨与下跌的天数

相关文章

评论 (0)