TypeError: unhashable type: 'numpy.ndarray' - 尝试绘图时出错

时间:2021-02-21 20:52:33

标签: python pandas numpy matplotlib

我正在尝试创建动画,但出现此错误:

File "/Users/x/PycharmProjects/pythonProject/venv/lib/python3.8/site-packages/matplotlib/category.py", line 209, in update
    for val in OrderedDict.fromkeys(data):
TypeError: unhashable type: 'numpy.ndarray'

可能是什么问题?代码如下:

import matplotlib.animation as ani
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os

dft = pd.read_csv('/Users/x/OneDrive/Learn/Analysis/Economy/E_T.csv')
dft.head(3)



dfrt = dft.drop(labels=0)

dfrt.rename(
    index=lambda x: dfrt.at[x,'Year'], inplace=True)

dfrt.index = pd.to_datetime(dfrt.index)


dfrt1 = dfrt[['EPS Growth', 'T Growth', 'Growth']]
dfrt1 = dfrt1.dropna()

dfrt1.info()

color = ['red', 'green', 'blue']
fig = plt.figure()

plt.xticks(rotation=45, ha="right", rotation_mode="anchor") #rotate the x-axis values
plt.subplots_adjust(bottom = 0.2, top = 0.9) #ensuring the dates (on the x-axis) fit in the screen
plt.ylabel('Yields')
plt.xlabel('Years')

def buildmebarchart1(i=int):
    plt.legend(dfrt1.columns)
    p = plt.plot(dfrt1[:i].index, dfrt1[:i].values) #note it only returns the dataset, up to the point i
    for i in range(0,3):
        p[i].set_color(color[i]) #set the colour of each curve

animator = ani.FuncAnimation(fig, buildmebarchart1)
plt.show()

0 个答案:

没有答案