【问题标题】:ValueError: x, y, and format string must not be NoneValueError:x、y 和格式字符串不能为 None
【发布时间】:2022-07-03 16:43:21
【问题描述】:

我想用指数移动平均线重新绘制一个图表,所以我在下面定义了平滑曲线。

def smooth_curve(points, factor=0.8):
  smoothed_points = []
  for point in points:
     if smoothed_points:
       previous = smoothed_points[-1]
       smoothed_points.append(previous * factor + point * (1 - factor))
     else:
       smoothed_points.append(point)
  return

我尝试使用matplotlib.pyplot as plt 进行绘图

plt.plot(epochs, smooth_curve(acc), 'bo', label='smoothed training acc')

但是我得到了下面的错误。

plt.plot(epochs, smooth_curve(acc), 'bo', label='smoothed training acc')
Traceback (most recent call last):

 Input In [119] in <cell line: 1>
 plt.plot(epochs, smooth_curve(acc), 'bo', label='smoothed training acc')

File C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py:2757 in plot
 return gca().plot(

File C:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py:1632 in plot
 lines = [*self._get_lines(*args, data=data, **kwargs)]

File C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py:312 in __call__
 yield from self._plot_args(this, kwargs)

File C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py:459 in _plot_args
 raise ValueError("x, y, and format string must not be None")

ValueError: x, y, and format string must not be None

我检查了smooth_curve(acc)编译成功。

【问题讨论】:

    标签: python matplotlib keras


    【解决方案1】:

    函数执行后需要返回smoothed_points值。如果您只使用return,该函数将返回一个None 空值,从而导致其余代码出现问题:

    def smooth_curve(points, factor=0.8):
      smoothed_points = []
      for point in points:
         if smoothed_points:
           previous = smoothed_points[-1]
           smoothed_points.append(previous * factor + point * (1 - factor))
         else:
           smoothed_points.append(point)
      return smoothed_points
    

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2018-09-10
      • 2015-05-18
      • 2020-10-27
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多