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

2024-05-13 04:26:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用多个字典在一个屏幕上绘制多个绘图(即,我尝试将24个不同的绘图,6乘4,作为一个文件绘制)。 我将所有相关绘图信息存储在字典histograms_dict_Wiki103, histograms_dict_billion_google, histograms_dict_openbookQA, histograms_dict_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>)}

此外,下面是我如何制作直方图词典的:

# 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()

谢谢,


Tags: oflayerobjectsgooglepltarraydictlist