Contourplot matplotlib 每第 n 条线颜色不同

Contourplot matplotlib with every n-th line a different color

是否可以在 Matplitlib 中创建一个等高线图,每第 n 条线的颜色与其余线的颜色不同?我想要这个,因为较少的轮廓线显示的细节不够多,而且所有相同的颜色使它过于拥挤。例如:

是的,你可以。您必须为每个级别指定颜色:

levels = np.logspace(0, np.log10(Z.max()), 100 )[30:80]
color_levels = ['r' if (i+5) % 10 == 0 else 'k' for i in range(len(levels))]
pyplot.contour(X, Y, Z, locator=ticker.LogLocator(), colors=color_levels, levels=levels, lw=2, norm=colors.LogNorm(), vmin=1, vmax=Z.max())

这里是Znp.histogram2d()的结果。底层 histogram2d 用 imshow 绘制。 [30:80] 级别上的切片是为了防止图像的中间和边缘混乱。

当然,您可以将 color_levels 定义中的 % 10 编辑为您喜欢的每个整数。

这导致: