[英]Python matplotlib contour plot logarithmic color scale
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. 然而,Matplotlib以一种奇怪的方式绘制颜色条 - 标签放置不好,只出现一种颜色。 The idea is based on http://adversus.110mb.com/?cat=8 这个想法基于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) 我使用最新的git-repository matplotlib版本,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: 它的工作原理很完美,正如你所建议的......但是,我有另一个问题:为什么matplotlib不允许我在对数模式中选择水平线的数量:
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()
http://i.stack.imgur.com/VeVFQ.png http://i.stack.imgur.com/VeVFQ.png
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. 相反,你应该得到所需的结果。
It looks like levels
expects increasing values. 看起来levels
期望值增加。 Try changing them to: levels=[1e-3, 1e-2, 1e-1, 1e0]
and see if that solves your issue. 尝试将它们更改为: levels=[1e-3, 1e-2, 1e-1, 1e0]
并查看是否能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.