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

使用Matplotlib绘制多组y值对应x值时仅显示两组曲线的问题

问题解决:Matplotlib仅显示两组曲线的修复方案

你的代码里plt.plot(x,y1,y2,y3)的参数传递逻辑错误,导致只画出两组曲线。matplotlib的plot函数要求每一组曲线对应一对(x, y)参数,你当前的写法会把y2识别为y1的样式参数,y3没有匹配的x轴数据,因此无法正常绘制。

下面是两种修复方法:

方法1:多次调用plot函数

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1, 0, 10)
y1 = 36.554 + 5.418 * np.exp(-1.327 * x) 
y2 = 23.816 + 18.088 * np.exp(-0.812 * x)
y3 = 20.562 + 21.389 * np.exp(-0.765 * x)

# 分别绘制每条曲线
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)

plt.show()  # 调用show()显示图像

方法2:单次plot传入多组(x,y)对

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1, 0, 10)
y1 = 36.554 + 5.418 * np.exp(-1.327 * x) 
y2 = 23.816 + 18.088 * np.exp(-0.812 * x)
y3 = 20.562 + 21.389 * np.exp(-0.765 * x)

# 一次性传入多组x-y对
plt.plot(x, y1, x, y2, x, y3)

plt.show()

可选优化:添加图例区分曲线

如果需要更清晰地区分三条曲线,可以添加标签和图例:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1, 0, 10)
y1 = 36.554 + 5.418 * np.exp(-1.327 * x) 
y2 = 23.816 + 18.088 * np.exp(-0.812 * x)
y3 = 20.562 + 21.389 * np.exp(-0.765 * x)

plt.plot(x, y1, label='y1')
plt.plot(x, y2, label='y2')
plt.plot(x, y3, label='y3')

plt.legend()  # 显示图例
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 23:20:27