带有for循环的单个图中的多个图

时间:2017-04-06 10:57:55

标签: python pandas matplotlib quandl

我试图在单个图中显示n个图,n是美国州号的数量。

编译器不喜欢这两行x[j] = df['Date'] y[j] = df['Value']

=> TypeError:'NoneType'对象不可订阅

import quandl
import pandas as pd
import matplotlib.pyplot as plt

states = pd.read_html('https://simple.wikipedia.org/wiki/List_of_U.S._states')
j = 0
x = []
y = []

for i in states[0][0][1:]:
        df = quandl.get("FMAC/HPI_"+i, authtoken="yourtoken" )
        df = df.reset_index(inplace=True, drop=False)
        x[j] = df['Date']
        y[j] = df['Value']
        j += 1

plt.plot(x[j],y[j])
plt.xlabel('Date')
plt.ylabel('Value')
plt.title('House prices')
plt.legend()
plt.show()

1 个答案:

答案 0 :(得分:1)

您遇到此特定错误的问题在于您正在使用inplace参数并分配回变量df。当使用inplace参数等于True时,返回值为None。

print(type(df.reset_index(inplace=True, drop=False)))
NoneType

print(type(df.reset_index(drop=False)))
pandas.core.frame.DataFrame

使用inplace=True并且不要分配回df:

df.reset_index(inplace=True, drop=False)

或使用默认值inplace = False并分配回变量df

df = df.reset_index(drop=False)

这里还有其他一些逻辑错误。

编辑以获得工作图表(测试限制为20)

for i in states[0][0][1:20]:
        df = quandl.get("FMAC/HPI_"+i, authtoken="yourtoken" )
        df.reset_index(inplace=True, drop=False)
        plt.plot('Date','Value',data=df)


# plt.plot(x[j],y[j])
plt.xlabel('Date')
plt.ylabel('Value')
plt.title('House prices')
plt.show()

enter image description here

相关问题