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

时间:2019-12-19 02:24:40

标签: python pandas matplotlib scatter

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

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

1 个答案:

答案 0 :(得分:1)

您可以尝试 plt.axvline(<value>,color='red',ls='--')plt.axhline(<value>,color='red',ls='--')。用您需要的行值替换