plt.plot label显示中文
时间: 2023-07-20 11:12:19 浏览: 195
如果要在 `plt.plot` 中显示中文标签,可以按照以下步骤操作:
1. 安装中文字体库:
```python
!sudo apt-get install -y fonts-wqy-zenhei
```
2. 在代码中指定字体为中文字体:
```python
import matplotlib.pyplot as plt
# 指定中文字体
plt.rcParams['font.family'] = ['WenQuanYi Zen Hei']
```
3. 在 `plt.plot` 中添加中文标签:
```python
import matplotlib.pyplot as plt
# 指定中文字体
plt.rcParams['font.family'] = ['WenQuanYi Zen Hei']
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.xlabel('横轴标签')
plt.ylabel('纵轴标签')
plt.title('标题')
plt.show()
```
这样就可以在 `plt.plot` 中显示中文标签了。
相关问题
python plt.plot label怎么设置
在Python中,使用Matplotlib库的plt.plot函数可以绘制折线图,并且可以通过设置label参数来添加标签。label参数用于给绘制的线条添加一个名称,以便在图例中显示。
下面是设置label的示例代码:
```python
import matplotlib.pyplot as plt
# 创建数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 绘制折线图并设置label
plt.plot(x, y, label='Line 1')
# 添加图例
plt.legend()
# 显示图形
plt.show()
```
在上述代码中,我们通过`label='Line 1'`将折线的标签设置为"Line 1"。然后使用`plt.legend()`函数添加图例,它会根据设置的label自动生成图例。
# 绘制损失函数曲线 plt.plot(train_losses, label='Train Loss') plt.plot(test_losses, label='Test Loss') plt.ylim([0, 1]) # 设置y轴范围为0~1 plt.legend() plt.show() # 绘制准确率曲线 plt.plot(train_accs, label='Train Acc') plt.plot(test_accs, label='Test Acc') plt.ylim([0, 1]) # 设置y轴范围为0~1 plt.legend() plt.show() 使曲线平滑
为了使曲线平滑,可以使用`plot()`函数的`smooth`参数,它可以控制曲线的平滑程度。可以将`smooth`参数设置为一个大于0的整数,代表对曲线进行平均滤波的窗口大小。较大的窗口大小会导致曲线更平滑,但可能会使曲线更加模糊。
以下是修改后的代码,将`plot()`函数的`smooth`参数设置为10:
``` python
import matplotlib.pyplot as plt
train_losses, train_accs, test_losses, test_accs = [], [], [], []
for epoch in range(100):
train_loss, train_acc = train(model, train_loader, criterion, optimizer)
test_loss, test_acc = test(model, test_loader, criterion)
train_losses.append(train_loss)
train_accs.append(train_acc)
test_losses.append(test_loss)
test_accs.append(test_acc)
print('Epoch: {} Train Loss: {:.4f} Train Acc: {:.4f} Test Loss: {:.4f} Test Acc: {:.4f}'.format(
epoch, train_loss, train_acc, test_loss, test_acc))
# 绘制损失函数曲线
plt.plot(train_losses, label='Train Loss', smooth=10)
plt.plot(test_losses, label='Test Loss', smooth=10)
plt.ylim([0, 1]) # 设置y轴范围为0~1
plt.legend()
plt.show()
# 绘制准确率曲线
plt.plot(train_accs, label='Train Acc', smooth=10)
plt.plot(test_accs, label='Test Acc', smooth=10)
plt.ylim([0, 1]) # 设置y轴范围为0~1
plt.legend()
plt.show()
```
这里将`plot()`函数的`smooth`参数设置为10,来对损失函数和准确率的曲线进行平滑处理。根据实际情况,可以适当调整`smooth`参数的大小,以得到更好的平滑效果。
阅读全文
相关推荐















