How can I draw scatter trend line on matplot? Python-Pandas

后端 未结 3 2084
天命终不由人
天命终不由人 2020-12-14 21:24

I want to draw a scatter trend line on matplot. How can I do that?

Python

import pandas as pd
import matplotlib.pyplot as plt
csv = pd.read_csv(\'/tm         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 22:04

    With text:

    from sklearn.metrics import r2_score
    
    plt.plot(x,y,"+", ms=10, mec="k")
    z = np.polyfit(x, y, 1)
    y_hat = np.poly1d(z)(x)
    
    plt.plot(x, y_hat, "r--", lw=1)
    text = f"$y={z[0]:0.3f}\;x{z[1]:+0.3f}$\n$R^2 = {r2_score(y,y_hat):0.3f}$"
    plt.gca().text(0.05, 0.95, text,transform=plt.gca().transAxes,
         fontsize=14, verticalalignment='top')
    

提交回复
热议问题