首先,我是Python新手,所以有很多我不知道。
我想做的是生成一个等高线图网格,每个网格图都是自定义 python 包(显然不是我写的)的输出,它消耗了大量资源。我可以一次制作一个这些图,没有问题,但如果我尝试一次制作多个图,例如:
fig, ax = plt.subplots(2, 2)
ax[0,0].contourf(blah blah)
ax[1,0].contourf(blah blah)
ax[0,1].contourf(blah blah)
ax[1,1].contourf(blah blah)
plt.show()
它窒息并给出“分配失败”错误。我一直在谷歌搜索寻找一种方法来命名每个图,以便以后可以调用它们并将它们插入到大网格中。要么确实没有办法做到这一点,要么我对行话太不熟悉,看不到它。
如有任何帮助,我们将不胜感激!
基本上,您会首先创建数据。
如果您不想重新计算数据,那么您可以加载它(例如从 csv 文件、数据库或其他源)。
所有的绘图都是在之后开始的。
因此,代码的结构如下所示:
import matplotlib.axes
import matplotlib.pyplot as plt
import numpy as np
######### the data goes here ##########
# in the case of the question this is pre-saved data
#######################################
# Generate coordinate grids (X,Y) and Z values.
x = np.linspace(-3, 3, 30)
y = np.linspace(-3, 3, 30)
X, Y = np.meshgrid(x, y)
Z = (1 - X/2 + X**5 + Y**3) * np.exp(-X**2 - Y**2)
######## the plot goes here ##########
# Create contour plot
fig, ax = plt.subplots()
ax: matplotlib.axes.Axes
CS = ax.contour(X, Y, Z)
ax.clabel(CS, inline=1, fontsize=10)
ax.set_title('Contour Plot')
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()