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)
你可以用 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])