import matplotlib.pyplot as plt
import numpy as np
# 生成点集x,1~20之间所有整数的数组
x = np.arange(1, 21)
# 使用subplot方式绘制
plt.figure(figsize=(12, 8))
# 图像1:蓝色长须线
plt.subplot(1,3, 1)
plt.plot(x, x, color='blue', linestyle='--', marker='None', label='Line 1')
plt.title('Image 1')
# 图像2:绿色正方形标记
plt.subplot(1, 3, 2)
plt.plot(x, x**2, color='green', marker='s', label='Line 2')
plt.title('Image 2')
# 图像3:红色短点相间线和星形标记
plt.subplot(1, 3, 3)
plt.plot(x, x**3, color='red', linestyle=(0, (1, 1)), marker='*', label='Line 3')
plt.title('Image 3')
# 调整子图间距
plt.tight_layout()
# 保存图像
plt.savefig('subplot_images.png')
# 显示图像
plt.show()
# 使用subplots方式绘制
fig, axs = plt.subplots(3, 1, figsize=(8, 18))
# 图像1:蓝色长须线
axs[0].plot(x, x, color='blue', linestyle='--', marker='None')
axs[0].set_title('Image 1')
# 图像2:绿色正方形标记
axs[1].plot(x, x**2, color='green', marker='s')
axs[1].set_title('Image 2')
# 图像3:红色短点相间线和星形标记
axs[2].plot(x, x**3, color='red', linestyle=(0, (1, 1)), marker='*')
axs[2].set_title('Image 3')
# 调整子图间距
plt.tight_layout()
# 保存图像
plt.savefig('subplots_images.png')
# 显示图像
plt.show()
Python 红绿蓝折线图
最新推荐文章于 2024-08-14 12:00:04 发布