如何使用Gridspec创建子图

huangapple go评论65阅读模式
英文:

How to create subplots using Gridspec

问题

以下是翻译好的代码部分:

import numpy as np
import matplotlib.pyplot as plt

# 为等高线图生成一些随机数据
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X ** 2 + Y ** 2) / 10)
Z2 = np.exp(-((X - 1) ** 2 + (Y - 1) ** 2) / 10)

# 创建图和子图
fig, axes = plt.subplots(3, 3, figsize=(12, 9))

# 在第一列中绘制填充等高线图
axes[0, 0].contourf(X, Y, Z1)
axes[1, 0].contourf(X, Y, Z2)
axes[2, 0].contourf(X, Y, Z1 + Z2)

# 在第二列中绘制填充等高线图
axes[0, 1].contourf(X, Y, Z1)
axes[1, 1].contourf(X, Y, Z2)
axes[2, 1].contourf(X, Y, Z1 + Z2)

# 为线图生成一些随机数据
x = np.linspace(0, 2 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = np.exp(-x)
y5 = np.sqrt(x)
y6 = x ** 2

# 在第三列中绘制堆叠的线图
axes[0, 2].plot(x, y1)
axes[1, 2].plot(x, y2)
axes[2, 2].plot(x, y3)
axes[0, 2].plot(x, y4)
axes[1, 2].plot(x, y5)
axes[2, 2].plot(x, y6)

# 为子图设置标题和标签
axes[0, 0].set_title('等高线图 1')
axes[0, 1].set_title('等高线图 2')
axes[0, 2].set_title('线图')

# 调整子图之间的间距
plt.subplots_adjust(hspace=0.3, wspace=0.3)

# 显示图
plt.show()

请注意,这是原始代码的翻译部分,包括了生成等高线图和线图的过程。希望这对你有所帮助。

英文:

How can I produce 3x3 subplots where the first and second columns are filled contour and the third column will have two stacked horizontal line plots in each of the rows?

This is the kind of layout (not to scale) that I want. Any generic answer will be good(https://i.stack.imgur.com/D58zL.png)

The code below is an example but I want the line plots in the third column to be in different panels instead of an overplot.

 import numpy as np
import matplotlib.pyplot as plt
# Generate some random data for the contour plots
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X ** 2 + Y ** 2) / 10)
Z2 = np.exp(-((X - 1) ** 2 + (Y - 1) ** 2) / 10)
# Create the figure and subplots
fig, axes = plt.subplots(3, 3, figsize=(12, 9))
# Plot filled contour in the first column
axes[0, 0].contourf(X, Y, Z1)
axes[1, 0].contourf(X, Y, Z2)
axes[2, 0].contourf(X, Y, Z1 + Z2)
# Plot filled contour in the second column
axes[0, 1].contourf(X, Y, Z1)
axes[1, 1].contourf(X, Y, Z2)
axes[2, 1].contourf(X, Y, Z1 + Z2)
# Generate some random data for the line plots
x = np.linspace(0, 2 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = np.exp(-x)
y5 = np.sqrt(x)
y6 = x ** 2
# Plot stacked line plots in the third column
axes[0, 2].plot(x, y1)
axes[1, 2].plot(x, y2)
axes[2, 2].plot(x, y3)
axes[0, 2].plot(x, y4)
axes[1, 2].plot(x, y5)
axes[2, 2].plot(x, y6)
# Set titles and labels for subplots
axes[0, 0].set_title('Contour 1')
axes[0, 1].set_title('Contour 2')
axes[0, 2].set_title('Line Plots')
# Adjust the spacing between subplots
plt.subplots_adjust(hspace=0.3, wspace=0.3)
# Show the plot
plt.show()

The line plot should have the same scale and also the contour plots.

答案1

得分: 0

你可以使用make_axes_locatable,然后使用append_axes附加坐标轴。

绘制线条的代码:

from mpl_toolkits.axes_grid1 import make_axes_locatable
# 在第三列绘制堆叠线图

axes[0, 2].plot(x, y1)
divider_0 = make_axes_locatable(axes[0, 2])
cax0 = divider_0.append_axes("bottom", size="100%", pad=0.25, sharex=axes[0, 2])
cax0.plot(x, y4)

axes[1, 2].plot(x, y2)
divider_1 = make_axes_locatable(axes[1, 2])
cax1 = divider_1.append_axes("bottom", size="100%", pad=0.25, sharex=axes[1, 2])
cax1.plot(x, y5)

axes[2, 2].plot(x, y3)
divider_2 = make_axes_locatable(axes[2, 2])
cax2 = divider_2.append_axes("bottom", size="100%", pad=0.25, sharex=axes[2, 2])
cax2.plot(x, y6)

图的输出:

如何使用Gridspec创建子图

更新

如果您想要更改列的相对宽度,可以在创建图形时使用width_ratios参数:

fig, axes = plt.subplots(3, 3, figsize=(12, 9), width_ratios=[1, 1, 2])

新的输出:

如何使用Gridspec创建子图

英文:

You can use make_axes_locatable then append axis with append_axes.

The codes for plotting lines :

from mpl_toolkits.axes_grid1 import make_axes_locatable
# Plot stacked line plots in the third column
axes[0, 2].plot(x, y1)
divider_0 = make_axes_locatable(axes[0, 2])
cax0 = divider_0.append_axes("bottom", size="100%", pad=0.25, sharex=axes[0, 2])
cax0.plot(x, y4)
axes[1, 2].plot(x, y2)
divider_1 = make_axes_locatable(axes[1, 2])
cax1 = divider_1.append_axes("bottom", size="100%", pad=0.25, sharex=axes[1, 2])
cax1.plot(x, y5)
axes[2, 2].plot(x, y3)
divider_2 = make_axes_locatable(axes[2, 2])
cax2 = divider_2.append_axes("bottom", size="100%", pad=0.25, sharex=axes[2, 2])
cax2.plot(x, y6)

The output of the figure :

如何使用Gridspec创建子图

Update

If you want to change the relative widths of the columns, you can use the argument width_ratios during the initialization of the figure:

fig, axes = plt.subplots(3, 3, figsize=(12, 9), width_ratios=[1, 1, 2])

The new output :

如何使用Gridspec创建子图

huangapple
  • 本文由 发表于 2023年7月12日 21:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定