我想在matplot上绘制一个散点趋势线.我怎样才能做到这一点?
蟒蛇
import pandas as pd import matplotlib.pyplot as plt csv = pd.read_csv('/tmp/test.csv') data = csv[['fee', 'time']] x = data['fee'] y = data['time'] plt.scatter(x, y) plt.show()
CSV
fee,time 100,650 90,700 80,860 70,800 60,1000 50,1200
时间是整数值.
散点图
对不起,我自己找到了答案.
如何在python matplotlib点(散点)图中添加趋势线?
蟒蛇
import pandas as pd import numpy as np import matplotlib.pyplot as plt csv = pd.read_csv('/tmp/test.csv') data = csv[['fee', 'time']] x = data['fee'] y = data['time'] plt.scatter(x, y) z = np.polyfit(x, y, 1) p = np.poly1d(z) plt.plot(x,p(x),"r--") plt.show()
图表