import matplotlib.pyplot as plt import numpy as np # 加载数据集 train_data = pd.read_csv('mnist_dataset/mnist_train.csv') test_data = pd.read_csv('mnist_dataset/mnist_test.csv') # 提取特征和标签 train_features, train_labels = train_data.iloc[:, 1:], train_data.iloc[:, 0] test_features, test_labels = test_data.iloc[:, 1:], test_data.iloc[:, 0] # 可视化前100张图片 def visualize_images(features, labels): fig, axs = plt.subplots(10, 10, figsize=(10, 10)) for i in range(10): for j in range(10): axs[i, j].imshow(np.array(features.iloc[i * 10 + j]).reshape(28, 28), cmap='gray') axs[i, j].axis('off') axs[i, j].set_title('Label: {}'.format(labels.iloc[i * 10 + j])) plt.show() # 可视化前100张训练集图片 visualize_images(train_features[:100], train_labels[:100])这段代码中怎么使得label输出的字体变小
时间: 2024-02-24 10:53:08 浏览: 154
你可以在 `set_title` 中设置 `fontsize` 参数来调整标签的字体大小,例如:
```python
import matplotlib.pyplot as plt
import numpy as np
# 加载数据集
train_data = pd.read_csv('mnist_dataset/mnist_train.csv')
test_data = pd.read_csv('mnist_dataset/mnist_test.csv')
# 提取特征和标签
train_features, train_labels = train_data.iloc[:, 1:], train_data.iloc[:, 0]
test_features, test_labels = test_data.iloc[:, 1:], test_data.iloc[:, 0]
# 可视化前100张图片
def visualize_images(features, labels):
fig, axs = plt.subplots(10, 10, figsize=(10, 10))
for i in range(10):
for j in range(10):
axs[i, j].imshow(np.array(features.iloc[i * 10 + j]).reshape(28, 28), cmap='gray')
axs[i, j].axis('off')
axs[i, j].set_title('Label: {}'.format(labels.iloc[i * 10 + j]), fontsize=8)
plt.show()
# 可视化前100张训练集图片
visualize_images(train_features[:100], train_labels[:100])
```
以上代码中,`fontsize=8` 表示设置字体大小为 8。你可以根据需要调整这个值。
阅读全文