matplotlib没有显示图表

时间:2016-07-20 18:48:21

标签: python matplotlib seaborn

我已编写此代码以显示折线图但该图未显示。窗口打开并显示标签和轴,但没有绘图。我不确定我做错了什么。也许这是一个我忽略的小错误

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns

df = pd.read_csv("passing_stats_1970_2016.csv", index_col=0)
df = df[pd.notnull(df['Season'])]

# print(qb_data.head())

avg_td = df.groupby('Season').TD.mean()


# setting up seaborn, creating white background
sns.set_style("white")

# setting height to 12, width to 9
plt.figure(figsize=(12,9))

# getting x and y values
x_values = df.Season.unique()
y_values = avg_td


# title
title = ("Average TD by season")

#Label y axis
plt.ylabel('Avg TDs', fontsize=18)

#Limit range of axis labels to only show where data is
plt.xlim(1966, 2014.5)
plt.ylim(0,0.08)

# create dashed lines
plt.grid(axis='y',color='grey', linestyle='--', lw=0.5, alpha=0.5)

# Change the size of tick labels for both axis
# to a more readable font size
plt.tick_params(axis='both', labelsize=14)

# get rid of borders for our graph using seaborn's
# despine function
sns.despine(left=True, bottom=True)

# plot the line for our graph
plt.plot(x_values, y_values)

plt.text(1966, -0.012,
         'Primary Data Source: http://www.basketball-reference.com/draft/'
         '\nAuthor: Joe T',
          fontsize=12)

# Display graph
plt.show()

这是我在打印x和y值时得到的结果:

[ 1970.  1971.  1972.  1973.  1974.  1975.  1976.  1977.  1978.  1979.
  1980.  1981.  1982.  1983.  1984.  1985.  1986.  1987.  1988.  1989.
  1990.  1991.  1992.  1993.  1994.  1995.  1996.  1997.  1998.  1999.
  2000.  2001.  2002.  2003.  2004.  2005.  2006.  2007.  2008.  2009.
  2010.  2011.  2012.  2013.  2014.  2015.]

Season    
1970.0    11.625000
1971.0     9.971429
1972.0    11.645161
1973.0     9.444444
1974.0     8.947368
1975.0    11.545455
1976.0    10.750000
1977.0     9.750000
1978.0    13.090909
1979.0    15.212121
1980.0    16.194444
1981.0    13.700000
1982.0     9.700000
1983.0    15.026316
1984.0    13.658537
1985.0    13.093023
1986.0    13.048780
1987.0    12.121951
1988.0    11.931818
1989.0    14.297297
1990.0    14.486486
1991.0    12.153846
1992.0    11.285714
1993.0    11.068182
1994.0    12.813953
1995.0    15.317073
1996.0    13.431818
1997.0    13.088889
1998.0    12.812500
1999.0    12.775510
2000.0    13.886364
2001.0    15.810811
2002.0    14.755556
2003.0    13.276596
2004.0    17.050000
2005.0    13.311111
2006.0    13.666667
2007.0    13.294118
2008.0    15.073171
2009.0    15.288889
2010.0    16.204545
2011.0    16.204545
2012.0    18.871795
2013.0    17.863636
2014.0    18.428571
2015.0    18.409091
Name: TD, dtype: float64

1 个答案:

答案 0 :(得分:0)

您的y轴上限为0.08,但您的y值在9-19范围内。