以下是一个利用matplotlib.pyplot创建一个包含9个子图的例子:

import matplotlib.pyplot as plt
import numpy as np

# 创建一个2x2的子图
fig, axs = plt.subplots(3, 3, figsize=(10, 10))
fig.suptitle('9 Subplots')

# 生成一些随机数据
x = np.linspace(-np.pi, np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.sqrt(x)

# 在每个子图中绘制不同的数据
axs[0, 0].plot(x, y1)
axs[0, 0].set_title('Sin(x)')
axs[0, 1].plot(x, y2)
axs[0, 1].set_title('Cos(x)')
axs[0, 2].plot(x, y3)
axs[0, 2].set_title('Sqrt(x)')
axs[1, 0].hist(y1, bins=10)
axs[1, 0].set_title('Histogram of Sin(x)')
axs[1, 1].scatter(x, y2)
axs[1, 1].set_title('Scatter of Cos(x)')
axs[1, 2].bar(x, y3)
axs[1, 2].set_title('Bar of Sqrt(x)')
axs[2, 0].plot(x, y1, color='r')
axs[2, 0].plot(x, y2, color='b')
axs[2, 0].set_title('Sin(x) and Cos(x)')
axs[2, 1].plot(x, y1, linestyle='--')
axs[2, 1].plot(x, y2, linestyle='-.')
axs[2, 1].set_title('Sin(x) and Cos(x) with different linestyles')
axs[2, 2].plot(x, y1, marker='o')
axs[2, 2].plot(x, y2, marker='*')
axs[2, 2].set_title('Sin(x) and Cos(x) with different markers')

# 调整子图之间的间距
plt.tight_layout()

# 显示图表
plt.show()

这个例子中,我们创建了一个2x2的子图,并在每个子图中绘制了不同的数据。我们使用了不同的绘图函数,例如plot,hist,scatter和bar,并使用了不同的参数来调整每个子图的样式。我们还使用了tight_layout函数来调整子图之间的间距。最后,我们使用show函数显示图表

利用matplotlibpyplot创建一个图表里面包含9个子图

原文地址: https://www.cveoy.top/t/topic/hwq9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录