Matplotlib趋势线代码。。将运行但不会绘制p(x)线。。。。我不知道怎么了。

2024-10-01 15:41:46 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮忙。。。我没法画线。它将绘制x,y散点图,但不会出现趋势线。在

import numpy as np
import matplotlib.pyplot as fs
name = ["SlowFast","Bedrock","MeanSlow","hAgDenkm","hSinDenkm","hAgSl","hAgFa","hSinSl","hSinF","LogSlowFast","LogBedrock","LogMeanSlow","LoghAgDenkm",#
             "LoghSinDenkm","LoghAgSl","LoghAgFa","LoghSinSl","LoghSinFa"]
data = np.genfromtxt('C:\Users\Ben\Documents\R\LWM_Study\LWM52714BigRun.csv',dtype = 'float' , delimiter = ',' , skip_header = 0, skip_footer= 20 , names = name) ### data array###

x=data["LogSlowFast"]
y=data["LoghSinDenkm"]
z = np.polyfit(x,y,1)
p = np.poly1d(z)
fs.plot(x,y,'ro',x,p(x),'r--')
fs.ylabel("a")
fs.xlabel("LogSlowFast")
fs.show()

print x,y,z,p

Tags: nameimportnumpydatamatplotlibasnp绘制
1条回答
网友
1楼 · 发布于 2024-10-01 15:41:46

您的数据xy一定有问题。如果将代码的第一部分替换为某些人工数据:

x=np.linspace(0,1,100)
y=x**2
z = np.polyfit(x,y,1)
p = np.poly1d(z)
fs.plot(x,y,'ro',x,p(x),'r ')
fs.ylabel("a")
fs.xlabel("LogSlowFast")
fs.show()

您将获得:

enter image description here

所以,除了数据之外,所有东西都像代码一样保存。在

关于数据的有趣之处:

  • x.dtype
  • y.dtype
  • 数据中是否可能有任何nan?在
  • p

我的猜测是数据中有一个nan或者可能是一个inf。这将使趋势线也变得nan,因此不可见。在

也许要验证的第一点是p的内容是否合理。在

相关问题 更多 >

    热门问题