简体   繁体   English

matplotlib等高线图与lognorm - colorbar水平

[英]matplotlib contour plot with lognorm - colorbar levels

I am trying to make a contour plot with defined levels and log norm. 我正在尝试使用定义的级别和日志规范制作等高线图。 Below is an example: 以下是一个例子:

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 = 10 * (Z1* Z2)

fig=plt.figure()
ax1 = fig.add_subplot(111)
lvls = np.logspace(-4,0,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()

在此输入图像描述 My questions is: 我的问题是:
The levels should be written in the format: '1x10^-4', '1.6x10^-4', ... How do i do this, without specifying each level manually? 级别应以以下格式写入:'1x10 ^ -4','1.6x10 ^ -4',...如何在不指定每个级别的情况下执行此操作?

I am using python 2.7.3 with matplotlib 1.1.1 on Windows 7. 我在Windows 7上使用python 2.7.3和matplotlib 1.1.1。

From here I found an approach that seems to fit your question: 这里我发现了一个似乎适合你的问题的方法:

from matplotlib.ticker import LogFormatter
l_f = LogFormatter(10, labelOnlyBase=False)
cbar = plt.colorbar(CF, ticks=lvls, format=l_f)

which will give: 这会给:

在此输入图像描述

note that the spacing between the ticks are indeed in log scale... 请注意,刻度之间的间距确实是对数刻度...

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

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