Рисование нескольких графиков на Python с использованием словарей - PullRequest
0 голосов
/ 08 января 2020

Я пытаюсь нарисовать несколько графиков на одном экране, используя несколько словарей (т.е. я пытаюсь нарисовать 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()

Спасибо,

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...