请你给下面的代码每行写出注释,要求详细易懂。 import matplotlib.pyplot as plt %matplotlib inline from matplotlib import gridspec fig = plt.figure(figsize=(20, 4.5)) gs = gridspec.GridSpec(1, 2, width_ratios=[1, 2.5]) ax1 = plt.subplot(gs[0]) ax2 = plt.subplot(gs[1]) counts = [data["num_train"], data["num_test"]] colors = ['silver', 'purple'] explode = (0.1, 0) # explode 1st slice labels = ['train','test'] ax1.pie(counts, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140) counts = [] labels = [] for namecode in data["class_name"].keys(): counts.append(data["class_info"][namecode]) labels.append(data["class_name"][namecode]) print(len(counts),len(labels)) print(counts) print(labels) df = pd.DataFrame({"labels": labels, "counts": counts}) ax2.bar(df["labels"], df["counts"]) ax2.set_title("nums") ax2.set_ylabel("% nums") # ax2.set_xticks(rotation=-15) ax2.set_xticklabels(labels = labels, rotation=-15) plt.show()
时间: 2024-01-21 19:04:19 浏览: 163
```python
# 导入 matplotlib.pyplot 模块,并设置在 jupyter notebook 内部显示
import matplotlib.pyplot as plt
%matplotlib inline
# 从 matplotlib 模块中导入 gridspec
from matplotlib import gridspec
# 创建一个大小为 (20, 4.5) 的图像对象
fig = plt.figure(figsize=(20, 4.5))
# 创建一个 1 行 2 列的网格,第一列的宽度为第二列的 1/2.5
gs = gridspec.GridSpec(1, 2, width_ratios=[1, 2.5])
# 在第一个子图中绘制饼图
ax1 = plt.subplot(gs[0])
# 在第二个子图中绘制柱状图
ax2 = plt.subplot(gs[1])
# 定义饼图中的数据和颜色
counts = [data["num_train"], data["num_test"]]
colors = ['silver', 'purple']
# 定义第一块数据的偏移量为 0.1
explode = (0.1, 0)
# 在第一个子图中绘制饼图,并设置标签、颜色、百分比格式、阴影、起始角度等
ax1.pie(counts, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
# 清空计数器和标签列表
counts = []
labels = []
# 遍历类别信息,获取每个类别的样本数和名称
for namecode in data["class_name"].keys():
counts.append(data["class_info"][namecode])
labels.append(data["class_name"][namecode])
# 输出类别的个数、样本数和名称
print(len(counts),len(labels))
print(counts)
print(labels)
# 将标签和计数放到一个 pandas 数据框中,并在第二个子图中绘制柱状图
df = pd.DataFrame({"labels": labels, "counts": counts})
ax2.bar(df["labels"], df["counts"])
ax2.set_title("nums")
ax2.set_ylabel("% nums")
# 设置 x 轴标签旋转角度为 -15 度
ax2.set_xticklabels(labels = labels, rotation=-15)
# 显示图像
plt.show()
```
阅读全文