Matplotlib图绘制直线,不显示正确的y值

时间:2019-09-28 07:49:20

标签: python pandas matplotlib

我想在x轴上绘制季节,在Green的Y轴上绘制管制百分比。该图显示不正确,如下所示:

incorrect plot

dj dataframe

如何获取matplotlib绘制正确的y轴值?

import pandas as pd
from matplotlib import pyplot as plt

pga = pd.read_csv('/Users/dpericks/Desktop/PGA_Data_Historical.csv')
dj = pga[(pga['Player Name'] == 'Dustin Johnson')
    & (pga['Variable'] == 'Greens in Regulation Percentage - (%)')]
plt.plot(dj['Season'], dj['Value'])

1 个答案:

答案 0 :(得分:1)

import pandas as pd
from matplotlib import pyplot as plt

pga = pd.read_csv('PGA_Data_Historical.csv')
dj = pga[(pga['Player Name'] == 'Dustin Johnson')
    & (pga['Variable'] == 'Greens in Regulation Percentage - (%)')]
dj_numberic_values_ = pd.to_numeric(dj['Value'])
plt.plot(dj['Season'], dj_numberic_values_)
plt.show()

调试您的代码时,我意识到我们从string type获得了dj['Value']个变量。我必须将这些值转换为适当的数值,以便matplotlib可以使用它们。希望对您有帮助。