【发布时间】:2016-07-21 19:28:34
【问题描述】:
我已经编写了这段代码来显示折线图,但该图没有显示出来。窗口打开并显示标签和轴,但没有绘图。我不确定我做错了什么。也许这是我忽略的一个小错误
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
【问题讨论】:
-
你能保存你的情节吗?尝试用
plt.savefig('image.png')替换plt.show(),看看是否出现了预期的情节。 -
打印
x_values和y_values并在此处发布。这可能只是您的轴限制不是您想要的问题。 -
@NickilMaveli 我保存了图像,但没有出现任何情节。它看起来就像窗户。只有轴标签。我添加了 x 和 y 值
-
首先我对
x和y值的输出感到困惑。为什么x_values元组或其他已经包含y值的东西?首先解决这个问题。x和y数组应该就是这样,列表或 numpy 数组或类似对象。x轴被标记为年份,但y_values似乎实际上是一系列年份。完全不一致。其次,我在您为ylim设置的[0,0.08]范围内的输出中看不到任何值。您的数据刚刚超出您的坐标轴范围。
标签: python matplotlib seaborn