# -*- 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.youkuaiyun.com/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