解释 self.ax8 = plt.subplot(self.gs[3, 2]) row_labels = [ 'Initial number of clients', 'Average connected clients', 'Average bandwidth usage', 'Average load factor of slices', 'Average coverage ratio', 'Average block ratio', 'Average handover ratio', ] l, r = self.xlim cell_text = [ [f'{len(self.clients)}'], [f'{mean(vals[l:r]):.2f}'], [f'{format_bps(mean(vals1[l:r]), return_float=True)}'], [f'{mean(vals2[l:r]):.2f}'], [f'{mean(vals4[l:r]):.2f}'], [f'{mean(vals5[l:r]):.4f}'], [f'{mean(vals6[l:r]):.4f}'], ] self.ax8.axis('off') self.ax8.axis('tight') self.ax8.tick_params(axis='x', which='major', pad=15) self.ax8.table(cellText=cell_text, rowLabels=row_labels, colWidths=[0.35, 0.2], loc='center right') plt.tight_layout()
时间: 2024-01-29 21:02:41 浏览: 62
这段代码是用来生成一个展示模拟结果的表格,包括7行数据,分别是:初始客户端数量、平均连接客户端数量、平均带宽使用量、平均切片负载因子、平均覆盖率、平均阻塞率、平均切换率。表格中每一行数据都是由模拟得到的统计数据组成的。其中 `self.ax8` 是用来绘制图形的子图对象,`row_labels` 是表格中每一行的名称,`cell_text` 是表格中每一个单元格的数据,`colWidths` 是每一列的宽度,`loc` 是表格的位置。`plt.tight_layout()`是用来自动调整子图之间的间距,使它们更加紧凑。
相关问题
def draw_stats(self, vals, vals1, vals2, vals3, vals4, vals5, vals6): self.ax1 = plt.subplot(self.gs[0, 0]) self.ax1.plot(vals) self.ax1.set_xlim(self.xlim) locs = self.ax1.get_xticks() locs[0] = self.xlim[0] locs[-1] = self.xlim[1] self.ax1.set_xticks(locs) self.ax1.use_sticky_edges = False self.ax1.set_title(f'Connected Clients Ratio') self.ax2 = plt.subplot(self.gs[1, 0]) self.ax2.plot(vals1) self.ax2.set_xlim(self.xlim) self.ax2.set_xticks(locs) self.ax2.yaxis.set_major_formatter(FuncFormatter(format_bps)) self.ax2.use_sticky_edges = False self.ax2.set_title('Total Bandwidth Usage') self.ax3 = plt.subplot(self.gs[2, 0]) self.ax3.plot(vals2) self.ax3.set_xlim(self.xlim) self.ax3.set_xticks(locs) self.ax3.use_sticky_edges = False self.ax3.set_title('Bandwidth Usage Ratio in Slices (Averaged)') self.ax4 = plt.subplot(self.gs[3, 0]) self.ax4.plot(vals3) self.ax4.set_xlim(self.xlim) self.ax4.set_xticks(locs) self.ax4.use_sticky_edges = False self.ax4.set_title('Client Count Ratio per Slice') self.ax5 = plt.subplot(self.gs[0, 1]) self.ax5.plot(vals4) self.ax5.set_xlim(self.xlim) self.ax5.set_xticks(locs) self.ax5.use_sticky_edges = False self.ax5.set_title('Coverage Ratio') self.ax6 = plt.subplot(self.gs[1, 1]) self.ax6.plot(vals5) self.ax6.set_xlim(self.xlim) self.ax6.set_xticks(locs) self.ax6.yaxis.set_major_formatter(FormatStrFormatter('%.3f')) self.ax6.use_sticky_edges = False self.ax6.set_title('Block ratio') self.ax7 = plt.subplot(self.gs[2, 1]) self.ax7.plot(vals6) self.ax7.set_xlim(self.xlim) self.ax7.set_xticks(locs) self.ax7.yaxis.set_major_formatter(FormatStrFormatter('%.3f')) self.ax7.use_sticky_edges = False self.ax7.set_title('Handover ratio') self.ax8 = plt.subplot(self.gs[3, 1]) row_labels = [ 'Initial number of clients', 'Average connected clients', 'Average bandwidth usage', 'Average load factor of slices', 'Average coverage ratio', 'Average block ratio', 'Average handover ratio', ] l, r = self.xlim cell_text = [ [f'{len(self.clients)}'], [f'{mean(vals[l:r]):.2f}'], [f'{format_bps(mean(vals1[l:r]), return_float=True)}'], [f'{mean(vals2[l:r]):.2f}'], [f'{mean(vals4[l:r]):.2f}'], [f'{mean(vals5[l:r]):.4f}'], [f'{mean(vals6[l:r]):.4f}'], ] self.ax8.axis('off') self.ax8.axis('tight') self.ax8.tick_params(axis='x', which='major', pad=15) self.ax8.table(cellText=cell_text, rowLabels=row_labels, colWidths=[0.35, 0.2], loc='center right') plt.tight_layout() 更改为只输出其中的第一行第一列的图片
抱歉,这段代码是用来绘制多张子图并将它们组合成一个大图的,不太适合只输出其中的一张子图。如果您只需要输出其中的一张子图,需要对代码进行相应修改,只绘制需要的子图并将其保存即可。例如,如果只需要输出第一行第一列的子图,可以将其余子图的代码注释掉,然后在最后加上保存图片的代码,比如:
```
def draw_stats(self, vals):
self.ax1 = plt.subplot(1, 1, 1)
self.ax1.plot(vals)
self.ax1.set_xlim(self.xlim)
locs = self.ax1.get_xticks()
locs[0] = self.xlim[0]
locs[-1] = self.xlim[1]
self.ax1.set_xticks(locs)
self.ax1.use_sticky_edges = False
self.ax1.set_title(f'Connected Clients Ratio')
plt.savefig('output.png', dpi=300)
```
这样,该函数将只绘制一个包含第一行第一列子图的图片,并将其保存为 'output.png' 文件。
(train_images, train_labels), (test_images, test_labels) = datasets.fashion_mnist.load_data() f = plt.figure(figsize=(12, 7)) f.suptitle('Label Counts for a Sample of Clients') client_data = collections.OrderedDict() for i in range(6): client_data[f'client_{i}'] = ( train_images[i*1000:(i+1)*1000], train_labels[i*1000:(i+1)*1000]) plot_data = collections.defaultdict(list) for example in client_data[f'client_{i}']: label = example[0].numpy() #images, labels = example[] #label = labels.numpy() plot_data[label].append(label) for i in range(6): plt.subplot(2, 3, i+1) plt.title('Client {}'.format(i)) for j in range(10): plt.hist( plot_data[j], density=False, bins=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
这段代码使用了TensorFlow中的fashion_mnist数据集,将训练集和测试集分别赋值给了(train_images, train_labels)和(test_images,_labels)。接着,使用matplotlib库绘制一个12*7的图像,标题为“一组客户的标签计数”。然后用OrderedDict创建了一个名为client_data的字典,其中包含6个键值对,每个键值对表示一个客户的图片和标签。最后用defaultdict创建了一个名为plot_data的字典,用于收集每位客户的标签计数。
阅读全文