子图可以最大化的呈现可用数据,py子图如何设置?
# 创建子图
fig, axs = plt.subplots(2, 2)
# 在第1行第1列的子图中绘制直方图
axs[0, 0].hist(x)
# 添加第1行第1列子图的标题和标签
axs[0, 0].set_title("Histogram of Data")
axs[0, 0].set_xlabel("Data Values")
axs[0, 0].set_ylabel("Frequency")
# 在第1行第2列的子图中绘制散点图
axs[0, 1].scatter(x, x + np.random.normal(size=1000))
# 添加第1行第2列子图的标题和标签
axs[0, 1].set_title("Scatterplot")
axs[0, 1].set_xlabel("X Values")
axs[0, 1].set_ylabel("Y Values")
# 在第2行第1列的子图中绘制箱线图
axs[1, 0].boxplot(x)
不对陈子图使用方法:
fig=plt.figure(figsize=(10,8)) fig.suptitle('城市流量情况') ax1 = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2,2,2) ax3 = fig.add_subplot(2,1,2)
不对称图表怎么生成
下面是使用 add_subplot() 函数创建 5 个子图,分成 3 行 2 列,其中第一个图占两个图位置的示例代码:
import matplotlib.pyplot as plt
# 创建一个 3 行 2 列的图形,并生成 5 个子图
fig = plt.figure(figsize=(8, 6))
ax1 = fig.add_subplot(3, 2, (1, 2))
ax2 = fig.add_subplot(3, 2, 3)
ax3 = fig.add_subplot(3, 2, 4)
ax4 = fig.add_subplot(3, 2, 5)
ax5 = fig.add_subplot(3, 2, 6)
# 绘制子图
ax1.plot([1, 2, 3], [4, 5, 6])
ax2.plot([1, 2, 3], [6, 5, 4])
ax3.plot([1, 2, 3], [2, 3, 1])
ax4.plot([1, 2, 3], [7, 8, 9])
ax5.plot([1, 2, 3], [1, 2, 3])
# 显示图形
plt.show()
在上述示例代码中,我们通过 plt.figure() 函数创建一个图形对象,并指定其大小为 (8, 6)。
然后,我们通过 fig.add_subplot(3, 2, (1, 2)) 函数来创建第一个子图,其中 (1, 2) 表示该子图占据网格的第 1 行第 1 列和第 1 行第 2 列。接下来,我们继续使用 fig.add_subplot() 函数来创建其余 4 个子图,分别占据网格的第 2 行第 1 列、第 2 行第 2 列、第 3 行第 1 列和第 3 行第 2 列。
接下来,我们在各个子图中绘制了一些简单的数据,并使用 plt.show() 函数显示图形。