构造散点图时索引超出范围的错误

时间:2018-11-23 10:46:53

标签: python pandas matplotlib visualization heatmap

我有一个形状为(25,19)的数据框。第1列有运行的bin,第2到19列是年。我想以此构建一个热图。

症状数据集

variable    bins    2000    2001    2002    2003    2004    2005    2005    2007    2008    2009    2010    2011    2012    2013    2014    2015    2016    2017
0           binA    2282447 2228013 2166100 2098020 2024397 1934590 1846291 1765541 1687144 1632758 1542434 1430951 1306639 1162500 996818  797653  554513  208297
1           binB    264863  279788  309941  333953  355826  380675  408065  435550  475923  530992  567297  602801  642232  686201  738248  804876  901286  1138540

... .........

我想按如下方式构建热图

enter image description here

但是,当我按照以下方式运行代码时,会给我索引错误。我理解错误,但是即使我将表截断以使其大小相等(19,19),我仍然会收到错误或只是空白图。

import matplotlib.pyplot as plt

cddfdf=cddfdf.iloc[:, 0:19]

fig, ax =plt.subplots()
ax.set_xticks(np.arange(cddfdf.shape[0]))
ax.set_yticks(np.arange(cddfdf.shape[0]))
ax.set_xticklabels(cddfdf['bins'])
ax.set_yticklabels(cddfdf['bins'])

# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
         rotation_mode="anchor")


# for i in range(renamedDfd.shape[0]):
#     for j in range(renamedDfd.shape[0]):
#         print(i, j)
#         renamedDfdd[i, j]
#         break
# renamedDfdd   



    #         text = ax.text(j, i, renamedDfdd[i, j],
    #                        ha="center", va="center", color="w")



    cddfdfd=cddfdf[['bins', 2000, 2001]]
    cddfdfdd=cddfdfd[[2000, 2001]]
    cddfdfddd=cddfdfdd.values

    len(cddfdfddd.T)

        #len(cddfdfddd['bins'])

“文本”行出现错误

 for i in range(cddfdfddd.shape[0]):
        for j in range(cddfdfddd.shape[0]):
            #print(i, j)
            from copy import deepcopy
            copyof = deepcopy(cddfdfddd.T)
            #copyof[i, j]
            #g=copyof.T
            #printå(g[i, j])
            text = ax.text(j, i, copyof[i, j],
                           ha="center", va="

center", color="`enter code here`w")

错误

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-59-85f53ea47f7c> in <module>()
      7         #g=copyof.T
      8         #print(g[i, j])
----> 9         text = ax.text(j, i, copyof[i, j],
     10                        ha="center", va="center", color="w")

IndexError: index 2 is out of bounds for axis 0 with size 2

我如何在(箱,箱)中的这个时间序列上构建热图

0 个答案:

没有答案