2020-08-27

    x=roe["日期"]
    x=[datetime.strptime(x, '%Y.%m.%d') for x in x]   #Try converting dates from string to date object first
    y=roe["率"]
    plt.plot(x, y, marker='o', mec='r', mfc='w',label=line_lable, linewidth=4)
    print(type(plt.gca()))
    plt.gca().yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))  #百分比显示
    plt.gca().xaxis.set_major_formatter(mdate.DateFormatter('%Y.%m.%d'))#设置时间标签显示格式
    #设置网格样式
    plt.gca().xaxis.grid(True, linestyle='-.')
    #plt.gca().set_xticklabels(x,fontsize=10,rotation=30)
    # zip joins x and y coordinates in pairs
    for a,b in zip(x,y):
        label = "{:.2%}".format(b)   #b代表轴数据
        plt.annotate(label, # this is the texts
                 (a,b), # this is the point to label
                 textcoords="offset points", # how to position the text
                 xytext=(0,10), # distance from text to points (x,y)
                 ha='center') # horizontal alignment can be left, right or center
    # 设置图表标题,并给坐标轴添加标签
    title=st+"_"+st_name+"_"+"趋势"
    #locs参数为数组参数(array_like, optional)
    locs=["2015.03.15",,"2020.06.30"]
    locs=[datetime.strptime(x, '%Y.%m.%d') for x in locs]   #Try converting dates from string to date object first
    #plt.xticks(pd.date_range('2014.03.15','2020.12.31',freq='90d'),rotation=90)
    plt.xticks(locs,rotation=90)
    #plt.xticks(rotation=90)
    plt.title("趋势比较",fontsize=20)
     #显示图形的图例
    plt.legend()
    plt.xlabel("日期", fontsize=12)
    plt.ylabel("率", fontsize=12)
    plt.tight_layout() #在原代码的画图部分的最后加上matplotlib自动调整的语句,图则会自动调整标签大小
    f_name="数据对比.jpg"
    csv_name=title+".csv"
    data =  "数据对比//"
    # 判断是否存在此文件夹
    folder = os.path.exists(data)
    # 如果不存在就新建该文件夹
    if not folder:
        os.makedirs(data)
    save_file = os.path.join(data, f_name)
    print(save_file)
    roe.to_csv(os.path.join(data, csv_name),encoding="utf_8_sig")
    plt.savefig(save_file)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。