简体   繁体   English

Contourplot matplotlib每第n条线使用不同的颜色

[英]Contourplot matplotlib with every n-th line a different color

Is it possible to create a contourplot in Matplitlib with every n-th line a different color then the rest? 是否可以在Matplitlib中创建一个等高线图,使第n条线的颜色与其余颜色不同? I would like to have this because this less contourlines are showing not enough detail, and all the same color makes it too crowded. 我想要这样,是因为轮廓线少了,显示的细节不足,而且所有相同的颜色使它太拥挤了。 For example: 例如: 银河没有花哨的轮廓线

Yes, you can. 是的你可以。 You have to specify the colors for each level: 您必须为每个级别指定颜色:

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())

Herein is Z the results of the np.histogram2d() . Znp.histogram2d()的结果。 The underlying histogram2d is plotted with imshow . 底层直方图imshowimshow绘制。 The [30:80] slice on the levels is to prevent cluttering in the middle and edges of the image. 水平上的[30:80]切片是为了防止图像的中间和边缘混乱。

Of course you can edit the % 10 in the definition of color_levels to every integer of your liking. 当然,您可以将color_levels定义中的% 10编辑为您喜欢的每个整数。

This results in: 结果是:

红色轮廓线的银河

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM