Python绘制双柱状图并显示数值

# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import matplotlib.pyplot as plt

# from pylab import mpl
# mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体
# mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

# 定义函数来显示柱状上的数值
def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x() + rect.get_width() / 2. - 0.2, 1.03 * height, '%s' % float(height))

if __name__ == '__main__':
    l1 = [0.1, 0.2, 0.3]
    l2 = [0.15, 0.25, 0.35]


    # name = ['正检率', '误检率', '漏检率']
    name = ['positive detection rate', 'false detecting rate', 'missing rate']

    total_width, n = 0.8, 2
    width = total_width / n
    x = [0, 1, 2]

    # a = plt.bar(x, l1, width=width, label='以往方法', fc='y')
    a = plt.bar(x, l1, width=width, label='previous method', fc='y')

    for i in range(len(x)):
        x[i] = x[i] + width
    # b = plt.bar(x, l2, width=width, label='本文方法', tick_label=name, fc='r')
    b = plt.bar(x, l2, width=width, label='suggested method', tick_label=name, fc='r')


    autolabel(a)
    autolabel(b)

    # plt.xlabel('检测方法')
    plt.xlabel('Detection Method')

    # plt.ylabel('结果')
    plt.ylabel('Results')

    plt.ylim(0, 1)
    # plt.title('实验结果')
    plt.title('Experimental Results')

    plt.legend()
    plt.show()


reference:https://blog.csdn.net/wobeatit/article/details/79826584

优化1:

# !/usr/bin/python
# -*- coding: utf-8 -*-

# *                     绘制单Y轴双变量柱状图                   *#

import re
import os  # os模块与文本操作直接相关的模块
import matplotlib.pyplot as plt
import numpy as np
# *********下面三句代码作用不详,就是为了防止出现编码问题*********
import importlib
import sys

# importlib.reload(sys)
# ****************************************************
font2 = {
   'family': 'Times New Roman',  # 设置字体
         'weight': 'normal',
         'size': 18,
         }


class Drawing:
    # 初始化
    def __init__(self):
        os.chdir('D:\\Learning\\Machine_Learning\\实习\\师姐论文实验')  # 改变工作目录到指定文件的目录
        self.content = open("acc-onlyRealImage-Iter2200-after.txt")
        self.content1 = open("acc-onlyRealImage-Iter2500-after.txt")

    def autolabel(self, rects, y):  # 在柱状图上面添加 数值
        i = 0
        for rect in rects:
            # 读出列表存储的value值
            value = y[i]
            x_1 = rect.get_x() + rect.get_width() / 2
            y_1 = rect.get_height()
            # x_1,y_1对应柱形的横、纵坐标
            i += 1
            plt.text(x_1, y_1, value, ha='center', va
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值