使用一个命令通过循环绘制大量轴对象 [英] plot a large number of axis objects using one command through a loop
问题描述
说我有一堆ax1,ax2,ax3 ...,我想通过绘图功能运行它们:
Say I have a bunch of ax1,ax2,ax3... and I want to run them through a plotting function:
def plotxy(ax,x,y):
x = np.array(x)
y = np.array(y)
ax.plot(x,y)
(明显简化了)
我如何不做就重复命令:
How would I repeat the command without doing:
plotxy(ax1,x,y)
plotxy(ax2,x,y)
...
plotxy(axn,x,y)
我确定有一种创建临时变量的方法,该变量将axn保持在循环中,循环可能只有一行.有任何想法吗?这将大大缩短我的代码,因为我需要用相同的命令绘制不同的东西,但是x& y以及多个子图.
I'm sure there is a way to create a temp variable which holds the axn in a loop which could be just one line. Any ideas? It would greatly shorten my code as I've got heaps of things I need to plot with the same command but differing x & y and over multiple subplots.
我想这是一个使用循环构造变量名的大问题的一部分吗?
I guess this is part of a large question of constructing variable names using a loop?
推荐答案
您可以尝试执行以下操作:
you can try something like this:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(nrows=3, ncols=2)
for ax in axs.flat:
plotxy(ax,x,y)
如果使用plt.subplot
或plt.axes
,则可以手动创建轴列表/数组
If you use plt.subplot
or plt.axes
you can create a list/array of axes by hand
这篇关于使用一个命令通过循环绘制大量轴对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!