# -*- 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()
优化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='bottom', fontdict={'size': 8}) # 在fontdict中设置字体大小
rect.set_edgecolor('white')
def Pictures(self):
lines = self.content.readlines()
lines_1 = self.content1.readlines()
x_1 = [line.strip().split(',')[0] for line in lines] # 字段以逗号分隔,这里取得是第1列
y_train_1 = [line.strip().split(',')[1] for line in lines]
y_train_2 = [line.strip().split(',')[1] for line in lines_1]
y_acc_1 = [line.strip().split(',')[4] for line in lines]
y_acc_2 = [line.strip().split(',')[4] for line in lines_1]
x = list(range(len(x_1)))
y_acc = []
y_acc1 = []
y_train = []
y_train1 = []
for i in range(len(y_acc_1)):
y_acc.append(float(y_acc_1[i]))
y_acc1.append(float(y_acc_2[i]))
y_train.append(int(y_train_1[i]))
y_train1.append(int(y_train_2[i]))
plt.xticks(x, x_1, rotation=0) # 设置X轴坐标值为label值
for i in range(len(x)): # 调整柱状图的横坐标,使得打印出来的图形看起来更加舒服
x[i] = x[i] - 0.2
a = plt.bar(x, y_train, width=0.4, label='iter2200', fc='b')
# a=plt.bar(x, y_acc,width=0.4,label='iter2200',fc = 'b')
for i in range(len(x)):
x[i] = x[i] + 0.4
b = plt.bar(x, y_train1, width=0.4, label='iter2500', fc='r')
# b=plt.bar(x, y_acc1, width=0.4, label='iter2500',fc = 'r')
plt.xlabel('Labels', font2)
# 设置Y轴值的范围
plt.ylim((0, 1000))
# 设置Y轴的刻度值
plt.yticks(np.arange(0, 1001, 200))
# plt.ylim((0, 1.1))
# plt.yticks(np.arange(0,1.1, 0.2))
# plt.ylabel('Accuracy',font2)
plt.ylabel('Number of training sets', font2) # 字体的格式在font2中有设置
self.autolabel(a, y_train_1) # 为柱形图打上数值标签
self.autolabel(b, y_train_2)
# self.autolabel(a,y_acc_1)
# self.autolabel(b,y_acc_2)
# plt.title("RealAndSimulation",font2)
plt.title("OnlyRealImage", font2)
plt.legend()
plt.show()
def run(self):
self.Pictures()
if __name__ == '__main__':
# Draw = Drawing()
# Draw.run()
# 下面开始是我的实验2019-2-21 10:14:53
def zul_autolabel( 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='bottom', fontdict={'size': 8}) # 在fontdict中设置字体大小
rect.set_edgecolor('white')
x_1 = ['a', 'b', 'c']
x = list(range(len(x_1)))
plt.xticks(x, x_1, rotation=0) # 设置X轴坐标值为label值
y1 = [0.1, 0.2, 0.3]
y2 = [0.1, 0.2, 0.3]
for i in range(len(x)): # 调整柱状图的横坐标,使得打印出来的图形看起来更加舒服
x[i] = x[i] - 0.2
a = plt.bar(x, y1, width=0.4, label='asd', fc='b')
for i in range(len(x)):
x[i] = x[i] + 0.4
b = plt.bar(x, y2, width=0.4, label='zxc', fc='r')
plt.xlabel('xx', font2)
# 设置Y轴值的范围
plt.ylim((0, 1))
# 设置Y轴的刻度值
# plt.yticks(np.arange(0, 1001, 200))
plt.ylabel('yy', font2) # 字体的格式在font2中有设置
zul_autolabel(a, y1) # 为柱形图打上数值标签
zul_autolabel(b, y2)
plt.title("ttt", font2)
plt.legend()
plt.show()
优化2:
# !/usr/bin/python
# -*- coding: utf-8 -*-
# * 绘制单Y轴双变量柱状图 *#
import re
import os # os模块与文本操作直接相关的模块
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter
# *********下面三句代码作用不详,就是为了防止出现编码问题*********
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='bottom', fontdict={'size': 8}) # 在fontdict中设置字体大小
rect.set_edgecolor('white')
def Pictures(self):
lines = self.content.readlines()
lines_1 = self.content1.readlines()
x_1 = [line.strip().split(',')[0] for line in lines] # 字段以逗号分隔,这里取得是第1列
y_train_1 = [line.strip().split(',')[1] for line in lines]
y_train_2 = [line.strip().split(',')[1] for line in lines_1]
y_acc_1 = [line.strip().split(',')[4] for line in lines]
y_acc_2 = [line.strip().split(',')[4] for line in lines_1]
x = list(range(len(x_1)))
y_acc = []
y_acc1 = []
y_train = []
y_train1 = []
for i in range(len(y_acc_1)):
y_acc.append(float(y_acc_1[i]))
y_acc1.append(float(y_acc_2[i]))
y_train.append(int(y_train_1[i]))
y_train1.append(int(y_train_2[i]))
plt.xticks(x, x_1, rotation=0) # 设置X轴坐标值为label值
for i in range(len(x)): # 调整柱状图的横坐标,使得打印出来的图形看起来更加舒服
x[i] = x[i] - 0.2
a = plt.bar(x, y_train, width=0.4, label='iter2200', fc='b')
# a=plt.bar(x, y_acc,width=0.4,label='iter2200',fc = 'b')
for i in range(len(x)):
x[i] = x[i] + 0.4
b = plt.bar(x, y_train1, width=0.4, label='iter2500', fc='r')
# b=plt.bar(x, y_acc1, width=0.4, label='iter2500',fc = 'r')
plt.xlabel('Labels', font2)
# 设置Y轴值的范围
plt.ylim((0, 1000))
# 设置Y轴的刻度值
plt.yticks(np.arange(0, 1001, 200))
# plt.ylim((0, 1.1))
# plt.yticks(np.arange(0,1.1, 0.2))
# plt.ylabel('Accuracy',font2)
plt.ylabel('Number of training sets', font2) # 字体的格式在font2中有设置
self.autolabel(a, y_train_1) # 为柱形图打上数值标签
self.autolabel(b, y_train_2)
# self.autolabel(a,y_acc_1)
# self.autolabel(b,y_acc_2)
# plt.title("RealAndSimulation",font2)
plt.title("OnlyRealImage", font2)
plt.legend()
plt.show()
def run(self):
self.Pictures()
if __name__ == '__main__':
# Draw = Drawing()
# Draw.run()
# 下面开始是我的实验2019-2-21 10:14:53
def zul_autolabel( 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='bottom', fontdict={'size': 11}) # 在fontdict中设置字体大小
rect.set_edgecolor('white')
x_1 = ['a', 'b', 'c']
x = list(range(len(x_1)))
plt.xticks(x, x_1, rotation=0) # 设置X轴坐标值为label值
y1 = [0.1, 0.2, 0.3]
y2 = [0.1, 0.2, 0.3]
y1_label = ['10%', '20%', '30%']
y2_label = ['10%', '20%', '30%']
for i in range(len(x)): # 调整柱状图的横坐标,使得打印出来的图形看起来更加舒服
x[i] = x[i] - 0.2
a = plt.bar(x, y1, width=0.4, label='asd', fc='b')
for i in range(len(x)):
x[i] = x[i] + 0.4
b = plt.bar(x, y2, width=0.4, label='zxc', fc='r')
plt.xlabel('xx', font2)
plt.ylim([0,1])
# 将坐标变成百分比形式
def to_percent(temp, position):
return '%1.0f' % (100 * temp) + '%'
plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
# 设置Y轴的刻度值
# plt.yticks(np.arange(0, 1001, 200))
plt.ylabel('yy', font2) # 字体的格式在font2中有设置
zul_autolabel(a, y1_label) # 为柱形图打上数值标签
zul_autolabel(b, y2_label)
plt.title("ttt", font2)
plt.legend()
plt.show()