Python 如何使用matplotlib创建子地块的大型图形?
我在遍历每个子地块时遇到问题。我到达子地块的坐标,然后希望在每个子地块上显示不同的模型。然而,我当前的解决方案循环遍历所有子地块,但在每个子地块上循环遍历所有模型,将最后一个模型绘制在每个子地块上,这意味着它们看起来都一样 我的目标是在每个子地块上放置一个模型。请帮忙Python 如何使用matplotlib创建子地块的大型图形?,python,matrix,matplotlib,plot,subplot,Python,Matrix,Matplotlib,Plot,Subplot,我在遍历每个子地块时遇到问题。我到达子地块的坐标,然后希望在每个子地块上显示不同的模型。然而,我当前的解决方案循环遍历所有子地块,但在每个子地块上循环遍历所有模型,将最后一个模型绘制在每个子地块上,这意味着它们看起来都一样 我的目标是在每个子地块上放置一个模型。请帮忙 modelInfo = csv_info(filename) # obtains information from csv file f, axarr = plt.subplots(4, 6) for i in range(4):
modelInfo = csv_info(filename) # obtains information from csv file
f, axarr = plt.subplots(4, 6)
for i in range(4):
for j in range(6):
for model in modelInfo:
lat = dictionary[str(model) + "lat"]
lon = dictionary[str(model) + "lon"]
lat2 = dictionary[str(model) + "lat2"]
lon2 = dictionary[str(model) + "lon2"]
axarr[i, j].plot(lon, lat, marker = 'o', color = 'blue')
axarr[i, j].plot(lon2, lat2, marker = '.', color = 'red')
axarr[i, j].set_title(model)
我认为这就是您要寻找的,只要
len(modelnfo)
小于6x4=24
,它就可以工作:
modelInfo = csv_info(filename) # obtains information from csv file
f, axarr = plt.subplots(4, 6)
for n, model in enumerate(modelInfo):
i = int(n/4)
j = n % 6
lat = dictionary[str(model) + "lat"]
lon = dictionary[str(model) + "lon"]
lat2 = dictionary[str(model) + "lat2"]
lon2 = dictionary[str(model) + "lon2"]
axarr[i, j].plot(lon, lat, marker = 'o', color = 'blue')
axarr[i, j].plot(lon2, lat2, marker = '.', color = 'red')
axarr[i, j].set_title(model)
您可以
zip
将模型和轴放在一起,并同时在两者上循环。但是,由于子图是作为2d数组来的
,因此首先必须对其元素进行“线性化”。通过对numpy
数组使用重塑
方法,您可以轻松地做到这一点。如果给该方法赋值-1
,它会将数组转换为1d
向量。由于缺少输入数据,我使用numpy
中的数学函数制作了一个示例。有趣的getattr
行就在那里,这样我就可以轻松地将标题添加到情节中:
from matplotlib import pyplot as plt
import numpy as np
modelInfo = ['sin', 'cos', 'tan', 'exp', 'log', 'sqrt']
f, axarr = plt.subplots(2,3)
x = np.linspace(0,1,100)
for model, ax in zip(modelInfo, axarr.reshape(-1)):
func = getattr(np, model)
ax.plot(x,func(x))
ax.set_title(model)
f.tight_layout()
plt.show()
结果如下所示:
请注意,如果您的型号数量超过可用的子批次数量
,则多余的型号将被忽略,且不会显示错误消息
希望这有帮助。太好了!比我现在做的好多了@你的GergesDib也会做这项工作,这是最重要的部分;)