【问题标题】:Plot graphs from csv file从 csv 文件绘制图形
【发布时间】:2021-03-07 07:45:58
【问题描述】:

我想绘制我的 csv 文件数据的图表
现在我想要 epoch 作为 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

【问题讨论】:

  • 请将示例数据发布为文本而不是图像。
  • 绘图命令看起来不错,问题一定是读取 csv,如果绘图结果为空,原因是值未附加到 x,y 列表中。试试 pandas,让整个任务变得简单

标签: python pandas tensorflow matplotlib


【解决方案1】:

使用df.plot

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()

输出

【讨论】:

    【解决方案2】:

    我想给出一个详细的答案,以便您了解如何绘制任何有效值。

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

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.arange(1,11)
    y = np.random.randn(10)
    z = y**2
    

    其次,制作图形对象:

    fig_sample, ax_sample = plt.subplots()
    

    这里 subplot 用于灵活地添加更多地块,或者您也可以只用于一个地块

    然后添加轴(只有当你想移动它时才需要它):

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

    设置标签:

    ax_sample.set_xlabel('x (epochs)')
    

    绘制第一个值 x vs y(或 epochs 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))
    

    【讨论】:

      猜你喜欢
      • 2016-08-08
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 2017-07-20
      • 2020-03-27
      • 1970-01-01
      相关资源
      最近更新 更多