python等高线区域染色_Python Matplotlib等高线图对数色标

I have problems with a contour-plot using logarithmic color scaling. I want to specify the levels by hand. Matplotlib, however, draws the color bar in a strange fashion -- the labels are not placed well and only one color appears. The idea is based on

http://adversus.110mb.com/?cat=8

Is there anybody out there, who can help me?

I use the latest git-repository matplotlib version, v1.1.0 (2011-04-21)

import matplotlib.pyplot as plt

import numpy as np

from matplotlib.mlab import bivariate_normal

from matplotlib.colors import LogNorm

from matplotlib.backends.backend_pdf import PdfPages

delta = 0.5

x = np.arange(-3.0, 4.001, delta)

y = np.arange(-4.0, 3.001, delta)

X, Y = np.meshgrid(x, y)

Z = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

#axim = ax.imshow(Z, norm = LogNorm())

axim = ax.contourf(X,Y,Z,levels=[1e0,1e-1,1e-2,1e-3],cmap=plt.cm.jet,norm = LogNorm())

cb = fig.colorbar(axim)

pp = PdfPages('fig.pdf')

pp.savefig()

pp.close()

plt.show()

Thank you very much for your help! It works perfect, as you suggested...

However, I have another question: Why does matplotlib not allow me to select the number of level lines in the logarithmic mode:

import matplotlib.pyplot as plt

import numpy as np

from matplotlib.mlab import bivariate_normal

from matplotlib.colors import LogNorm

from matplotlib.backends.backend_pdf import PdfPages

delta = 0.5

x = np.arange(-3.0, 4.001, delta)

y = np.arange(-4.0, 3.001, delta)

X, Y = np.meshgrid(x, y)

Z = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

#axim = ax.imshow(Z, norm = LogNorm())

#axim = ax.contourf(X,Y,Z,levels=[1e-3,1e-2,1e-1,1e0],cmap=plt.cm.jet,norm = LogNorm())

axim = ax.contourf(X,Y,Z,20,cmap=plt.cm.jet,norm = LogNorm())

cb = fig.colorbar(axim)

pp = PdfPages('fig.pdf')

pp.savefig()

pp.close()

plt.show()

This was my original problem...

解决方案

So it's easily fixed; your order of levels means that the lowest level gets drawn last and therefore covered everything!

Try:

axim = ax.contourf(X,Y,Z,levels=[1e-3, 1e-2, 1e-1, 1e0],cmap=plt.cm.jet,norm = LogNorm())

instead and you should get the desired result.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值