如果我理解正确,您有一个 x 值列表,并且每个 x 都有一些 y 相关联,每个 x,y 都有一个特定的 z 值。
z 值属于有限集(或可以四舍五入以确保只有有限集)。
所以,我创建了 x、y 和 z 的副本,并通过 z 同时对它们进行排序。
然后,遍历 z 数组,收集 x,y,每次 z 变化时,都可以绘制属于该颜色的所有线。
为了不需要特殊的步骤来绘制最后一组线,我附加了一个标记。
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(150000,550000,100000)
y = np.random.rand(7*4)
z = [0.6,0.6,0.6,0.6,0.7,0.7,0.7,0.7,0.8,0.8,0.8,0.8,0.9,0.9,0.9,0.9,1.0,1.0,1.0,1.0,1.1,1.1,1.1,1.1,1.2,1.2,1.2,1.2]
z_min = min(z)
z_max = max(z)
x_ = np.hstack([x,x,x,x,x,x,x])
zs = [round(c, 1) for c in z] # make sure almost equal z-values are exactly equal
zs, xs, ys = zip( *sorted( zip(z, x_, y) ) ) # sort the x,y via z
zs += (1000,) # add a sentinel at the end that can be used to stop the line drawing
xs += (None, )
ys += (None, )
plt.set_cmap('plasma')
cmap = plt.get_cmap() # get the color map of the current plot call with `plt.get_cmap('jet')`
norm = plt.Normalize(z_min, z_max) # needed to map the z-values between 0 and 1
plt.scatter(x_, y, c=z, zorder=10) # z-order: plot the scatter dots on top of the lines
prev_x, prev_y, prev_z = None, None, None
x1s, y1s, x2s, y2s = [], [], [], []
for x0, y0, z0 in zip(xs, ys, zs):
if z0 == prev_z:
x1s.append(prev_x)
y1s.append(prev_y)
x2s.append(x0)
y2s.append(y0)
elif prev_z is not None: # the z changed, draw the lines belonging to the previous z
print(x1s, y1s, x2s, y2s)
plt.plot(x1s, y1s, x2s, y2s, color=cmap(norm(prev_z)))
x1s, y1s, x2s, y2s = [], [], [], []
prev_x, prev_y, prev_z = x0, y0, z0
plt.colorbar()
plt.show()
这是你的意思吗?