fig, ax = plt.subplots(1,2) ax[0].plot(history.history['loss'], color='b', label="Training loss") ax[0].plot(history.history['val_loss'], color='r', label="Testing loss",axes =ax[0]) legend = ax[0].legend(loc='best', shadow=True) ax[1].plot(history.history['accuracy'], color='b', label="Training accuracy") ax[1].plot(history.history['val_accuracy'], color='r',label="Testing accuracy") legend = ax[1].legend(loc='best', shadow=True) plt.title('Loss and Accuracy of DNN Model') plt.show()
时间: 2023-10-11 18:06:42 浏览: 182
这是一个使用Matplotlib绘制的DNN模型的训练和测试损失图以及训练和测试准确率图,其中训练损失和准确率用蓝色线表示,测试损失和准确率用红色线表示。第一行代码中的1表示创建一个包含1个子图的Figure对象,2表示将该Figure对象分成一行两列,ax[0]和ax[1]则表示第一个子图和第二个子图。第二行代码中,使用plot函数绘制了训练损失和测试损失的曲线,其中label参数用于指定曲线的名称。第三行代码中,使用legend函数添加图例并指定其位置和阴影效果。第四行代码中,绘制了训练准确率和测试准确率的曲线,并添加了相应的图例及其位置和阴影效果。最后一行代码中,使用title函数添加图表标题,并使用show函数将图表展示出来。
相关问题
fig, ax1 = plt.subplots(
fig, ax1 = plt.subplots()是用于创建一个包含一个子图的Figure对象和一个Axes对象的函数[^1]。其中,Figure对象代表整个图形窗口,而Axes对象则代表一个具体的绘图区域。
下面是一个示例,演示了如何使用fig, ax1 = plt.subplots()创建一个包含两个子图的图形窗口,并在每个子图中绘制不同的数据[^2]:
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# 创建图形窗口和子图
fig, ax1 = plt.subplots()
# 在第一个子图中绘制数据
ax1.plot(x, y1, 'b', lw=1.5, label='1st')
ax1.plot(x, y1, 'ro')
ax1.grid(True)
ax1.legend(loc=0)
ax1.set_xlabel('index')
ax1.set_ylabel('value 1st')
ax1.set_title('A Simplot plot')
# 创建第二个子图
ax2 = ax1.twinx()
# 在第二个子图中绘制数据
ax2.plot(x, y2, 'g', lw=1.5, label='2nd')
ax2.plot(x, y2, 'ro')
ax2.legend(loc=0)
ax2.set_ylabel('value 2nd')
# 显示图形
plt.show()
```
这段代码会创建一个包含两个子图的图形窗口,其中第一个子图绘制了y1的数据,第二个子图绘制了y2的数据。每个子图都有自己的y轴,但共享x轴。在绘制完数据后,使用plt.show()函数显示图形。
fig,ax1=plt.subplots()
`fig, ax1 = plt.subplots()` 是 Python 中 Matplotlib 库中的一个常用语句,用于创建一个新的图形窗口(figure)并添加第一个子图(subplot)。当你看到这样的代码片段,通常是在准备进行数据可视化分析:
- `plt` 指的是 `matplotlib.pyplot`,这是一个模块,包含了大量绘图函数。
- `subplots()` 函数负责生成一个新的图像窗口,并返回两个元素:`fig` 表示新的 figure 对象,它是图形的整体容器;`ax1` 是一个 axes 或 axis 对象,代表了子图的实例,你可以在这个轴上绘制图形。
例如,如果你想要在一个窗口里创建一个简单的线形图,可以这样做:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 30, 40]
fig, ax1 = plt.subplots()
ax1.plot(x, y) # 在 ax1 上画线
# 可选操作,如设置标题、标签等
ax1.set_title('My First Plot')
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Y-axis')
plt.show() # 显示图形
```
阅读全文
相关推荐













