使用Python Matplotlib库绘制直方图、曲线图和折线图

将数据保存在excel表格中,用xlrd读取xlsx表格文件中的数据,将之绘图。
首先安装依赖库

Matplotlib 3.0+ does not support Python 2.x, 3.0, 3.1, 3.2, 3.3, or 3.4.
Beginning with Matplotlib 3.0, Python 3.5 and above is required.

pip3 install xlrd
pip3 install matplotlib

曲线图

  1. 从excel文件中读取数据
data = xlrd.open_workbook('./VGGNet_prune.xlsx')
kd = data.sheets()[0] #sheet1
epochs = kd.col_values(0) #sheet1的第一列
train_acc = kd.col_values(1) #sheet1的第二列
  1. 画图
fig,ax = plt.subplots()
plt.title('VGGNet16 Training Accuracy')
ax.plot(epochs[1:-1],train_acc[1:-1], label='train dataset Accuracy')

ax.grid(which='major', axis='x', linewidth=0.75, linestyle='-', color='0.75')
ax.grid(which='minor', axis='x', linewidth=0.25, linestyle='-', color='0.75')
ax.grid(which='major', axis='y', linewidth=0.75, linestyle='-', color='0.75')
ax.grid(which='minor', axis='y', linewidth=0.25, linestyle='-', color='0.75')

ax.legend(loc='lower right') #显示标签
plt.show()

曲线图结果

直方图

# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
size = 25
# a = np.random.random(size)
# b = np.random.random(size)
# c = np.random.random(size)
# d = np.random.random(size)
train = [1109,1091,855,1063,1410,1650,1650,792,1190,1050,1398,894,779,1012,1080,1152,1314,1094,2093,1304,1121,1047,1080,1610,912]
val = [473,467,217,445,538,672,702,354,486,681,607,377,346,435,400,494,562,497,902,542,499,450,406,700,424]
# label = ['Apple', 'Banana', 'Birds', 'Cattle', 'Goat', 'Hamimelon', 'Jasmine', 'Laptop', 'Lily', 'Persons', 'Plum_blossom',
#      'Rhododendron', 'Rose', 'Strawberry', 'Watermelon', 'big_truck', 'bus', 'cars', 'cats', 'chrysanthemum', 'desks',
#      'dogs', 'peach_blossom', 'peony_flower', 'trees']
label = ['Apple','Banana','Big_truck','Birds','Bus','Cars','Cats','Cattle','Chrysanthemum','Desks','Dogs','Goat','Hamimelon',
         'Jasmine','Laptop','Lily','Peach_blossom','Peony_flower','Persons','Plum_blossom','Rhododendron','Rose','Strawberry','Trees','Watermelon']
# label.sort(reverse=True)
# x = np.arange(size)
y = np.arange(size)
fig=plt.figure()
ax=fig.add_subplot(111)

total_width, n = 1, 2     # 有多少个类型,只需更改n即可
width = total_width / n
# x = x - (total_width - width) / 2
y = y - (total_width - width) / 2
# ax.set_xticklabels(label, rotation=90);
# plt.xticks(x,label)
# plt.bar(x, train,  width=width, data=label,label='train_dataset')
# plt.bar(x + width, val, width=width, label='val_dataset')

ax.set_yticklabels(label, rotation=0,minor=False);
plt.yticks(y,label)
plt.barh(y,train,  height=width, data=label,label='train_dataset')
plt.barh(y + width,val, height=width, data=label,label='test_dataset')

plt.legend()
plt.show()

在这里插入图片描述

3

from matplotlib import pyplot as plt

x = range(15)
acc = [0.8855,0.8906, 0.8770, 0.8761, 0.88489,0.8879,0.891597,0.89341,0.88875,0.87857,0.8996, 0.887573,0.902169, 0.90098,0.89783]
plt.plot(x,acc)

plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值