Contourplot matplotlib,每第 n 行不同的颜色 [英] Contourplot matplotlib with every n-th line a different color
问题描述
是否可以在Matplitlib中创建一个轮廓图,使第n条线的颜色与其余颜色不同?我想要这个,因为这个较少的轮廓线显示的细节不够,而且所有相同的颜色使它太拥挤.例如:
Is it possible to create a contourplot in Matplitlib with every n-th line a different color then the rest? 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())
这里是 Z
np.histogram2d()
的结果.使用 imshow
绘制底层 histogram2d.水平上的 [30:80]
切片是为了防止图像的中部和边缘混乱.
Herein is Z
the results of the np.histogram2d()
. The underlying histogram2d is plotted with imshow
. The [30:80]
slice on the levels is to prevent cluttering in the middle and edges of the image.
当然,您可以将color_levels
定义中的% 10
编辑为您喜欢的每个整数.
Of course you can edit the % 10
in the definition of color_levels
to every integer of your liking.
结果是:
这篇关于Contourplot matplotlib,每第 n 行不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!