You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

绘制简单图表触发ValueError错误,寻求解决方案(附代码)

Fixing the ValueError in Your Matplotlib Plot

Hey there! That error is popping up because your function y(t) isn't returning any value—let's sort that out quickly.

The Root Cause

Your function definition is missing a return statement. When you call y(time), since there's no explicit return, it defaults to sending back None instead of the array of cosine values you calculated. Matplotlib's plot() can't handle a None for the y-axis data, which is exactly why you're seeing that VALUE ERROR: x, y, and format string must not be None.

Corrected Code

Here's the fixed version of your code:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

time = np.linspace(0, 2, 500)
pi = np.pi

def y(t):
    return np.cos((2 * pi * 10 * t) + 30)  # Added return statement here

plt.plot(time, y(time))
plt.show()  # Optional but explicit way to render the plot

What Changed?

  • Added the return keyword in the y(t) function so it actually outputs the computed cosine values instead of None.
  • Added plt.show() to ensure the plot renders clearly (while %matplotlib inline handles this in Jupyter environments, it's still good practice to include for consistency).

Run this code, and you should see your cosine wave plot without any errors!

内容的提问来源于stack exchange,提问作者eoinc837

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 08:32:32