从csv文件打印图形

2024-04-26 03:32:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想绘制我的csv文件数据的图表
现在我想把历元作为x轴,在y轴上,标签“acc”和“val_acc”是绘图,我尝试了下面的代码,但它给出了空白的图形 `

x = []
y = []

with open('trainSelfVGG.csv','r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')
    for row in plots:
        x.append('epoch')
        y.append('acc')

plt.plot(x,y, label='Loaded from file!')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Accuracy VS Val_acc')
plt.legend()
plt.show()`

我是python新手,请帮助csv文件的数据如下

epoch|  acc|    |   loss      |lr       |val_acc     |val_loss
0   0.712187529 0.923782527 5.00E-05    0.734799922 0.865529358
1   0.746874988 0.845359206 5.00E-05    0.733945608 0.870365739
2   0.739687502 0.853801966 5.00E-05    0.734799922 0.869380653
3   0.734375    0.872551799 5.00E-05    0.734799922 0.818775356
4   0.735000014 0.817328095 5.00E-05    0.744980752 0.782691181
5   0.738125026 0.813450873 5.00E-05    0.743200898 0.756890059
6   0.749842465 0.769637883 5.00E-05    0.746404648 0.761445224
7   0.740312517 0.779146731 5.00E-05    0.750605166 0.74676168
8   0.745937526 0.77233541  5.00E-05    0.738217294 0.754457355
9   0.760239422 0.717389286 5.00E-05    0.756656706 0.719709456
10  0.758437514 0.727203131 5.00E-05    0.753880084 0.766058266
11  0.756562471 0.718854547 5.00E-05    0.764060915 0.699205279
12  0.751874983 0.735785842 5.00E-05    0.76099956  0.711962938
13  0.762187481 0.709208548 5.00E-05    0.762850642 0.701643765
14  0.766250014 0.689858377 5.00E-05    0.771037996 0.698576272
15  0.791562498 0.642151952 5.00E-05    0.775665641 0.674562693
16  0.773750007 0.672213078 5.00E-05    0.77153641  0.683691561
17  0.785312474 0.657182395 5.00E-05    0.778015077 0.670122385

18  0.770951509 0.685499191 5.00E-05    0.774384141 0.670817852
19  0.777812481 0.673273861 5.00E-05    0.785134554 0.652816713
20  0.80250001  0.626691639 5.00E-05    0.783141136 0.66740793
21  0.787500024 0.64432466  5.00E-05    0.788053513 0.651966989
22  0.7890625   0.621332884 5.00E-05    0.775096118 0.663884819
23  0.787500024 0.637105942 5.00E-05    0.785775304 0.657734036
24  0.794580996 0.616357446 5.00E-05    0.771749973 0.670413017
25  0.803717732 0.599221408 5.00E-05    0.788195908 0.64291203
26  0.811874986 0.587966204 5.00E-05    0.791186094 0.653984845
27  0.804062486 0.591458261 5.00E-05    0.792538822 0.642165542
28  0.797187507 0.602103412 5.00E-05    0.78812474  0.635053933
29  0.807187498 0.595692158 5.00E-05    0.77474016  0.661368072
30  0.811909258 0.577990949 5.00E-05    0.774526536 0.668637931
31  0.820625007 0.546454251 5.00E-05    0.783212304 0.650670886
32  0.82593751  0.53596288  5.00E-05    0.778655827 0.651631236
33  0.805608094 0.582103312 5.00E-05    0.792823553 0.635468125
34  0.822621286 0.555304945 5.00E-05    0.783924222 0.647240341
35  0.823125005 0.551530778 5.00E-05    0.783141136 0.662788212

Tags: 文件csv数据csvfile图表绘制pltval
2条回答

使用^{}

import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv('trainSelfVGG.csv')
df[['epoch', 'acc', 'val_acc']].plot(
    x='epoch',
    xlabel='x',
    ylabel='y',
    title='Accuracy VS Val_acc'
)

plt.show()

输出

enter image description here

我想给出一个详细的答案,这样您就可以理解如何绘制任何有效值

首先,导入所需的库并制作一个假数据集:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(1,11)
y = np.random.randn(10)
z = y**2

第二,制作一个figure对象:

fig_sample, ax_sample = plt.subplots()

此处使用子图可灵活添加更多绘图,也可仅用于一个绘图

然后添加轴(仅当您要移动轴时才需要):

ax_sample = fig_sample.add_axes([0, 0, 1, 1])

设置标签:

ax_sample.set_xlabel('x (epochs)')

绘制第一个值x vs y(或历元vs acc)

ax_sample.plot(x, y, label = 'x-y plot')

添加图例以显示哪个图形代表什么:

ax_sample.legend(loc=(.8,.8))

对同一轴上的其他绘图重复以下步骤:

ax_sample.plot(x, z, label = 'x-z plot')
ax_sample.legend(loc=(.8,.8))

相关问题 更多 >