如何在具有个人功能的子图中绘制?

How to plot in subplots with personal function?

我想使用我创建的函数(例如 plot_graph(chosen_time))为子图中的每个轴绘制一张图。它会是这样的:

times = ['2010-01-01','2010-09-01','2011-01-01']
fig, axs = plt.subplots(3,1)
for i in times:
    axs[?].plot_graph(i)

我正在使用 xarray 并在 facets 上进行了尝试,但我发现了一些对我的目的的限制。

提前致谢。

我不确定这个示例是否符合您的要求,但希望对您有所帮助。

在您的代码中,您不能写成axs[?],而是需要使用axs[i]

import matplotlib.pyplot as plt

# fuction to plot i-th graph
def plot_graph(i, x, y, z, time):
    axs[i].plot(x, y, z)

fig, axs = plt.subplots(3, 1, sharex=True)

# set x, y, z, times
x = [[1, 2], [3, 4], [5, 6]]
y = [[1, 2], [3, 4], [5, 6]]
z = ["red", "blue", "yellow"]
times = ["2010-01-01", "2010-09-01", "2011-01-01"]


# plot i-th subplot
for i in range(3):    
    plot_graph(i, x[i], y[i], z[i], times[i])

plt.show()