一种解决方案是为包含实际数据的数据数组生成所有可能的索引组合。下面是一些示例代码,可能看起来很复杂,但主要是生成和绘制数据。
有 3 个数据集(如您所建议的),具有以下形式:
lat1 -> 点集
lat2 -> 点集的集合
lat3 -> 点
代码如下:
import matplotlib.pyplot as plt
import numpy as np
import itertools
# First data type
lat1 = np.array([np.linspace(0,180,100) for i in range(5)])
lon1 = np.array([10+5*np.random.random(100) for i in range(5)])
# lat1.shape = (5,100)
# [set][point]
# Second data type
lat2 = np.array([np.linspace(0,180,100) for i in range(5*3)]).reshape((3,5,100))
lon2 = np.array([30+10*np.random.random(100) for i in range(5*3)]).reshape((3,5,100))
# lat2.shape = (3,5,100)
# [collection][set][point]
# Third data type
lat3 = np.linspace(0,180,100)
lon3 = 50+5*np.random.random(100)
# lat3.shape = (100,)
# [point]
def plotter(xs,ys,ax,**kwargs):
# Get number of dimensions
ndim = xs.ndim
# Iterate over each dimension, generating all indices
if ndim>1:
indices = [np.arange(0,i,1) for i in xs.shape[:-1]]
comb = list(itertools.product(*indices))
else:
# This is to deal with one dimensional data (i.e. just a list)
comb = [slice(0, xs.shape[0])]
for c in comb:
xx = xs[c]
yy = ys[c]
ax.plot(xx, yy, **kwargs)
return ax
fig = plt.figure()
ax = fig.add_subplot(111)
plotter(lat1, lon1, ax, c='r', label='Type 1')
plotter(lat2, lon2, ax, c='b', label='Type 2')
plotter(lat3, lon3, ax, c='g', label='Type 3')
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
ax.legend(fontsize=8,bbox_to_anchor=(1,1))
ax.set_ylim(0,60)
ax.set_xlim(0,180)
fig.show()
给出下图: