是否有可能像下面的图片一样,在对数刻度中有色条的级别?
下面是一些可以实现的示例代码:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
delta = 0.025
x = y = np.arange(0, 3.01, delta)
X, Y = np.meshgrid(x, y)
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 1e6 * (Z1* Z2)
fig=plt.figure()
ax1 = fig.add_subplot(111)
lvls = np.logspace(0,4,20)
CF = ax1.contourf(X,Y,Z,
norm = LogNorm(),
levels = lvls
)
CS = ax1.contour(X,Y,Z,
norm = LogNorm(),
colors = 'k',
levels = lvls
)
cbar = plt.colorbar(CF, ticks=lvls, format='%.4f')
plt.show()
我在Windows7上使用Python2.7.3和MatplotLib1.1.1。
我建议生成一个伪彩色条,如下所示(有关解释,请参见注释):
这会给你一个这样的情节:
编辑
或者,在调用
colorbar
时使用新级别和spacing='proportional'
选项:替换此行:
有了这些:
替换此行:
有了这个:
最后你会得到这个情节:
(仅更改了
format
,因为新的记号不需要4个小数)编辑2
如果您希望自动生成与我使用的级别类似的级别,可以考虑以下代码:
相关问题 更多 >
编程相关推荐