使用字典在Python上绘制多个绘图

使用字典在Python上绘制多个绘图,python,dictionary,matplotlib,Python,Dictionary,Matplotlib,我尝试使用多个字典在一个屏幕上绘制多个绘图(即,我尝试将24个不同的绘图,6乘4,作为一个文件绘制)。 我将所有相关绘图信息存储在字典histograms\u dict\u Wiki103、histograms\u dict\u billion\u google、histograms\u dict\u openbookQA、histograms\u dict\u ARC下 下面是我用来让它工作的代码: from matplotlib import pyplot as plt def plot_

我尝试使用多个字典在一个屏幕上绘制多个绘图(即,我尝试将24个不同的绘图,6乘4,作为一个文件绘制)。 我将所有相关绘图信息存储在字典
histograms\u dict\u Wiki103、histograms\u dict\u billion\u google、histograms\u dict\u openbookQA、histograms\u dict\u ARC

下面是我用来让它工作的代码:

from matplotlib import pyplot as plt

def plot_figures_kendalltau_Corr(figures_dict_list, nrows, ncols):

    fig, axeslist = plt.subplots(ncols=ncols, nrows=nrows)
    fig.suptitle('kendalltau Corr. between TVD and attention weight of each token', fontsize=13)

    for j in range(len(figures_dict_list)):

        figures_dict = figures_dict_list[j]

        for ind, title in enumerate(figures_dict):
            axeslist.ravel()[ind].imshow(figures_dict[title], cmap=plt.gray())
            axeslist.ravel()[ind].set_title(title)
            axeslist.ravel()[ind].set_axis_off()

    plt.close()

hist_dict_list_kendalltau = [histograms_dict_Wiki103, histograms_dict_billion_google, histograms_dict_openbookQA, histograms_dict_ARC]

plot_figures_kendalltau_Corr(hist_dict_list_kendalltau, nrows=8, ncols=3) 
plt.savefig('kendalltau_corr_l.png') 
plt.close()
但这给了我一个错误信息,说:

TypeError: Image data of dtype object cannot be converted to float
下面是我拥有的其中一本词典的示例:

 {'1th layer, 1 Billion Google': (array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.,
          0., 0., 0.]),
   array([0.5 , 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95, 1.  ,
          1.05, 1.1 , 1.15, 1.2 , 1.25, 1.3 , 1.35, 1.4 , 1.45, 1.5 ]),
   <a list of 20 Patch objects>),
  '2th layer, 1 Billion Google': (array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0.]),
   array([0.  , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 , 0.45, 0.5 ,
          0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95, 1.  ]),
   <a list of 20 Patch objects>),
  '3th layer, 1 Billion Google': (array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0.]),
   array([0.  , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 , 0.45, 0.5 ,
          0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95, 1.  ]),
   <a list of 20 Patch objects>),
  '4th layer, 1 Billion Google': (array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0.]),
   array([0.  , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 , 0.45, 0.5 ,
          0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95, 1.  ]),
   <a list of 20 Patch objects>),
  '5th layer, 1 Billion Google': (array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0.]),
   array([0.  , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 , 0.45, 0.5 ,
          0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95, 1.  ]),
   <a list of 20 Patch objects>),
  '6th layer, 1 Billion Google': (array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0.]),
   array([0.  , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 , 0.45, 0.5 ,
          0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95, 1.  ]),
   <a list of 20 Patch objects>)}

谢谢,

axeslist.ravel()[ind].imshow(figures\u dict[title],cmap=plt.gray())中观察
figures\u dict[title]
。字典中的所有值是否都是图像数组?似乎其中一些不是,因此出现错误
TypeError:dtype对象的图像数据无法转换为float
。您好,谢谢您的评论,我编辑了我的问题以使其更清楚。如何将它们制作为图像阵列?谢谢,您可以看到
plt.hist
返回
(n,bin,patches)
,您正试图将该元组传递给
imshow()
imshow()
需要类似数组或PIL图像。这就是错误的原因。您好,再次感谢您的评论。那么,我应该如何修改代码以使其正常工作呢?我对imshow不太熟悉;我应该使用什么来代替plt.hist?是否要在网格形式的多个子图上绘制直方图?
# e.g. 1 billion google dataset

def label_yielder_billion_google(n):
    for j in range(1,n+1):
        yield(f"{j}th layer, 1 Billion Google")

labels_billion_google = [x for x  in label_yielder_billion_google(nlayer)]

histograms_dict_billion_google = {}

for j in range(nlayer):
    histogram_billion_google_j = plt.hist(kendalltau_tensor_billion_google_at_layer_j, bins = 20) 
    histograms_dict_billion_google[labels_billion_google[j]] = histogram_billion_google_j

plt.close()