使用 LogNorm() 时颜色条不显示值

发布于 2024-12-20 10:11:07 字数 753 浏览 4 评论 0原文

我正在尝试绘制等高线图,其中等高线水平按值的对数缩放。但是,颜色栏在颜色旁边没有显示足够的值。这是一个简单的例子。

import numpy as N
import matplotlib as M
import matplotlib.pyplot as PLT

# Set up a simple function to plot 
values = N.empty((10,10))
for xi in range(10):
    for yi in range(10):
        values[xi,yi] = N.exp(xi*yi/10. - 1)

levels = N.logspace(-1, 4, 10)
log_norm = M.colors.LogNorm() 
# Currently not used - linear scaling
linear_norm = M.colors.Normalize()

# Plot the function using the indices as the x and y axes
PLT.contourf(values, norm=log_norm, levels=levels)
PLT.colorbar()

如果您在轮廓函数调用中将 log_norm 切换为 Linear_norm,您将看到颜色条确实有值。当然,使用 Linear_norm 意味着颜色是线性缩放的,并且该函数的轮廓分布不均匀。

我正在 Mac OS 10.7 上使用 python 2.7.2,即 matplotlib 附带的 enthought 版本。

I am trying to make a contour plot with the contour levels scaled by the log of the values. However, the colorbar does not show enough values next to the colors. Here is a simple example.

import numpy as N
import matplotlib as M
import matplotlib.pyplot as PLT

# Set up a simple function to plot 
values = N.empty((10,10))
for xi in range(10):
    for yi in range(10):
        values[xi,yi] = N.exp(xi*yi/10. - 1)

levels = N.logspace(-1, 4, 10)
log_norm = M.colors.LogNorm() 
# Currently not used - linear scaling
linear_norm = M.colors.Normalize()

# Plot the function using the indices as the x and y axes
PLT.contourf(values, norm=log_norm, levels=levels)
PLT.colorbar()

If you switch log_norm for linear_norm in the contourf call, you'll see that the colorbar does have values. Of course, using linear_norm means the colors are scaled linearly and the contours are not well distributed for this function.

I'm using python 2.7.2, enthought edition which comes with matplotlib, on Mac OS 10.7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2024-12-27 10:11:07

PLT.colorbar 的调用添加格式:

import numpy as N
import matplotlib as M
import matplotlib.pyplot as PLT

# Set up a simple function to plot 
x,y = N.meshgrid(range(10),range(10))
values = N.exp(x*y/10. - 1)

levels = N.logspace(-1, 4, 10)
log_norm = M.colors.LogNorm() 
# Currently not used - linear scaling
linear_norm = M.colors.Normalize()
# Plot the function using the indices as the x and y axes
PLT.contourf(values, norm=log_norm, levels=levels)
PLT.colorbar(format='%.2f')
PLT.show()

在此处输入图像描述

Add a format to the call to PLT.colorbar:

import numpy as N
import matplotlib as M
import matplotlib.pyplot as PLT

# Set up a simple function to plot 
x,y = N.meshgrid(range(10),range(10))
values = N.exp(x*y/10. - 1)

levels = N.logspace(-1, 4, 10)
log_norm = M.colors.LogNorm() 
# Currently not used - linear scaling
linear_norm = M.colors.Normalize()
# Plot the function using the indices as the x and y axes
PLT.contourf(values, norm=log_norm, levels=levels)
PLT.colorbar(format='%.2f')
PLT.show()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文