在 python 的 for 循环中创建绘图时如何绘制子批次?
How to plot sublots when creating plots in a for loop in python?
我是编程新手,目前坚持这一点:我使用 for 循环创建了 4 个不同的图,我想将每个图分配给不同的子图。基本上,我想要 2x2 网格中的 4 个图,以便比较我的数据。任何人都知道我怎么能做到这一点?我的方法是创建一个子图列表,然后使用嵌套图将每个图分配给一个子图:
import matplotlib.pyplot as plt
import numpy as np
def load_file(filename):
return np.loadtxt(filename, delimiter=',', usecols=(0, 1), unpack=True, skiprows=1)
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax_list=[ax1, ax2, ax3, ax4]
for i,filename in enumerate(file_list):
for p in ax_list:
x,y = load_file(filename)
plt.plot(x,y,
label=l, #I assign labels from a list beforehand, as well as colors
color=c,
linewidth=0.5,
ls='-',
)
p.plot()
问题是,所有地块都只分配给一个子地块,我不知道如何更正这个问题。如果有任何帮助,我将不胜感激!
您不需要遍历文件名和情节,只需要 select 列表中的下一个情节。
for i, filename in enumerate(file_list):
p = ax_list[i]:
x,y = load_file(filename)
p.plot(x, y,
label=l, #I assign labels from a list beforehand, as well as colors
color=c,
linewidth=0.5,
ls='-')
plt.plot()
你也可以替换
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax_list=[ax1, ax2, ax3, ax4]
只有
fig, ax_list = plt.subplots(2, 2)
ax_list = ax_list.flatten()
获得一个简单的 2x2 网格。
我想你想要的是在所有 4 个图上显示不同的数据,因此使用单个循环。确保使用轴绘图方法,而不是 plt.plot
因为后者总是在最后一个子图中绘制。
import matplotlib.pyplot as plt
import numpy as np
def load_file(filename):
return np.loadtxt(filename, delimiter=',', usecols=(0, 1), unpack=True, skiprows=1)
fig, ax_list = plt.subplots(2,2)
for i,(filename,ax) in enumerate(zip(file_list, ax_list.flatten())):
x,y = load_file(filename)
ax.plot(x,y, linewidth=0.5,ls='-' )
plt.show()
我是编程新手,目前坚持这一点:我使用 for 循环创建了 4 个不同的图,我想将每个图分配给不同的子图。基本上,我想要 2x2 网格中的 4 个图,以便比较我的数据。任何人都知道我怎么能做到这一点?我的方法是创建一个子图列表,然后使用嵌套图将每个图分配给一个子图:
import matplotlib.pyplot as plt
import numpy as np
def load_file(filename):
return np.loadtxt(filename, delimiter=',', usecols=(0, 1), unpack=True, skiprows=1)
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax_list=[ax1, ax2, ax3, ax4]
for i,filename in enumerate(file_list):
for p in ax_list:
x,y = load_file(filename)
plt.plot(x,y,
label=l, #I assign labels from a list beforehand, as well as colors
color=c,
linewidth=0.5,
ls='-',
)
p.plot()
问题是,所有地块都只分配给一个子地块,我不知道如何更正这个问题。如果有任何帮助,我将不胜感激!
您不需要遍历文件名和情节,只需要 select 列表中的下一个情节。
for i, filename in enumerate(file_list):
p = ax_list[i]:
x,y = load_file(filename)
p.plot(x, y,
label=l, #I assign labels from a list beforehand, as well as colors
color=c,
linewidth=0.5,
ls='-')
plt.plot()
你也可以替换
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
ax_list=[ax1, ax2, ax3, ax4]
只有
fig, ax_list = plt.subplots(2, 2)
ax_list = ax_list.flatten()
获得一个简单的 2x2 网格。
我想你想要的是在所有 4 个图上显示不同的数据,因此使用单个循环。确保使用轴绘图方法,而不是 plt.plot
因为后者总是在最后一个子图中绘制。
import matplotlib.pyplot as plt
import numpy as np
def load_file(filename):
return np.loadtxt(filename, delimiter=',', usecols=(0, 1), unpack=True, skiprows=1)
fig, ax_list = plt.subplots(2,2)
for i,(filename,ax) in enumerate(zip(file_list, ax_list.flatten())):
x,y = load_file(filename)
ax.plot(x,y, linewidth=0.5,ls='-' )
plt.show()