我在pandas中有一个数据框,用于生成散点图,并希望包含该图的回归线。现在我正试着用polyfit做这个。
这是我的代码:
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from numpy import *
table1 = pd.DataFrame.from_csv('upregulated_genes.txt', sep='\t', header=0, index_col=0)
table2 = pd.DataFrame.from_csv('misson_genes.txt', sep='\t', header=0, index_col=0)
table1 = table1.join(table2, how='outer')
table1 = table1.dropna(how='any')
table1 = table1.replace('#DIV/0!', 0)
# scatterplot
plt.scatter(table1['log2 fold change misson'], table1['log2 fold change'])
plt.ylabel('log2 expression fold change')
plt.xlabel('log2 expression fold change Misson et al. 2005')
plt.title('Root Early Upregulated Genes')
plt.axis([0,12,-5,12])
# this is the part I'm unsure about
regres = polyfit(table1['log2 fold change misson'], table1['log2 fold change'], 1)
plt.show()
但我得到了以下错误:
TypeError: cannot concatenate 'str' and 'float' objects
有人知道我哪里出错了吗?我也不确定如何将回归线添加到我的绘图中。任何其他关于我的代码的一般性评论也会非常感谢,我仍然是一个初学者。
而不是替换'DIV/0!'手动将数据强制为数字。这同时做两件事:确保结果是数值类型(而不是str),并用
NaN
替换任何不能被解析为数字的条目。示例:这应该能纠正你的错误。但是,关于将一条线与数据拟合的一般主题,我使用了两种方法,这两种方法比polyfit更适合我。二者中的第二个更健壮(并且可能返回更多关于统计信息的详细信息),但是它需要stats模型。
策划它,做一些类似的事情
相关问题 更多 >
编程相关推荐