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

使用Python从TXT文件绘制图表却出现异常对角线求助

解决matplotlib绘图出现奇怪红线的问题

问题出在你代码里的plt.plot()这一行——这是个无参数的绘图调用,matplotlib会默认生成一条多余的折线,也就是你看到的奇怪红线。

直接删掉这行多余的代码即可,修改后的完整代码如下:

import matplotlib.pyplot as plt
import numpy as np

file_name = 0
data = np.genfromtxt('File.txt', unpack=True, usecols=range(5))
x = data[0]

y1 = data[1] # the first graph
y2 = data[2] # the second graph
y3 = data[3] # the third graph
y4 = data[4] # the fourth graph

plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.plot(x, y4)
plt.xlabel('Action')
plt.ylabel('Rate')
plt.title('Plot of Rate')
plt.legend()
plt.show()

额外建议:可以给每个plt.plot()添加label参数,让图例显示更清晰,比如:

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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 17:05:02