解释 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 15:02:41 浏览: 32
这段代码是用来生成一个展示模拟结果的表格,包括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' 文件。
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: label = example[1].numpy() plot_data[label].append(label) 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])该段代码出现了 'str' object has no attribute 'numpy'错误,该怎么修改
根据错误提示,看起来是尝试在一个字符串对象上使用 `.numpy()` 方法,导致出现该错误。因此,我们需要找出哪个变量是字符串类型并且应该使用 `.numpy()` 方法。
根据代码分析,我们可以看到变量 `example` 是一个字符串类型,它是一个键,用于在 `client_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:
images, labels = client_data[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])
```
在这个版本的代码中,我们对于每个 `client_data` 的键,先获取对应的值,即 `(images, labels)`,然后对 `labels` 应用 `.numpy()` 方法来获取标签张量。最后,所有标签都被存储在 `plot_data` 中,以便绘制直方图。
同时,我们还修改了 `plt.subplot()`、`plt.title()` 和 `range()` 中的某些参数来修复其他错误。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)