我已经成功绘制了一系列散点图。
import pandas as pd
import matplotlib
tips = pd.DataFrame([20, 10, 50, 60, 90, 20, 30, 15, 75, 35], columns = ['Tips'])
tips.index += 1
tips.index.name = 'Meals'
tips.reset_index().plot.scatter(x=tips.index.name, y='Tips', label='Tip Amount')
现在,我试图将系列的平均值(40.5)绘制为一条平线,但无法这样做。
这是相同代码的尝试
ax = tips.mean().plot()
tips.reset_index().plot.scatter(x=tips.index.name, y='Tips', label='Tip Amount', ax=ax)
答案 0 :(得分:0)
您可以使用axhline()函数
import pandas as pd
import matplotlib.pyplot as plt
tips = pd.DataFrame([20, 10, 50, 60, 90, 20, 30, 15, 75, 35], columns = ['Tips'])
tips.index += 1
tips.index.name = 'Meals'
plot = tips.reset_index().plot.scatter(x=tips.index.name, y='Tips', label='Tip Amount')
plot.axhline(tips.mean()[0])