Matplotlib pandas-绘制平均线到散点图?

Mathplotlib pandas-Plotting average line to scatter plot?

我有一个从 pandas 数据框的两列创建的散点图,我想在每个轴上添加一条线来表示平均值。这可以用散点图实现吗?

plt.title("NFL Conversion Rates", fontsize=40)

# simulating a pandas df['team'] column
types = df.Tm
x_coords = df['3D%']
y_coords = df['4D%']
binsy = [15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85]
binsx = [30,35,40,45,50,55]

avg_y = y_coords.mean()
avg_y = round(avg_y, 1)
display(avg_y)

avg_x = x_coords.mean()
avg_x = round(avg_x, 1)
display(avg_x)


for i,type in enumerate(types):
    x = x_coords[i]
    y = y_coords[i]
    plt.scatter(x, y, s=30, marker='o', edgecolor='black', cmap='purple', linewidth=1, alpha = 0.5)
    plt.text(x+0.2, y+0.1, type, fontsize=14)
    plt.xlabel('3rd Down Conversion Percentage',fontsize=30)
    plt.ylabel('4th Down Conversion Percentage', fontsize=30)
    plt.xticks(binsx)
    plt.yticks(binsy)

你可以试试 plt.axvline(<value>,color='red',ls='--')plt.axhline(<value>,color='red',ls='--')。替换为您想要行

的值