绘制平均线到散点图?

2024-04-25 16:36:46 发布

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

我有一个散点图从熊猫数据帧的两列创建,我想在每个轴上添加一条代表平均值的线。用散点图可以吗?你知道吗

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)

enter image description here


Tags: dftypedisplaypltcoordsmeantypesavg