【问题标题】:How to plot in subplots with personal function?如何在具有个人功能的子图中绘图?
【发布时间】:2020-10-27 03:08:46
【问题描述】:

我想使用我创建的函数(例如 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,但我发现了一些限制。

提前谢谢你。

【问题讨论】:

  • 你有 x, y 每次都绘制吗?还有,plot_graph 是什么?
  • plot_graph() 是我创建的一个函数,用于绘制数据集中特定时间的空间轮廓(x 是经度,y 是纬度,z(颜色)是所需变量)。我写这篇文章的目标是知道我是否可以使用带有子图的函数来绘制每个子图轴上的每次时间。

标签: python matplotlib subplot python-xarray


【解决方案1】:

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

在您的代码中,您不能写成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()

【讨论】:

  • 这真的很有帮助!谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-03-10
  • 2020-10-09
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 2016-07-30
  • 2021-10-28
相关资源
最近更新 更多