Python/Matplotlib - 颜色条范围和显示值

发布于 2024-11-04 04:12:54 字数 683 浏览 3 评论 0原文

当将 matplotlib 与等值线图一起使用时,我无法按我想要的方式显示颜色条。我读过很多类似的例子,但仍然无法得到我想要的。

在下图中,我想要改变两件事。我希望在颜色条上显示最小值和最大值(最大值应为 2.0,最小值应为 -0.1)。这两个值应该位于颜色条的最边缘。另外,我希望颜色栏在每次颜色过渡时显示值。例如。在下图中,在 2.1 和 1.8 之间,存在另一个不显示值的颜色过渡。

我想我可能需要使用规范,但到目前为止它对我不起作用。

在此处输入图像描述

代码:

import numpy as np
import matplotlib.pyplot as plt

xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.array([[0., 1.0, 2.0],
               [0., 1.0, 2.0],
               [-0.1, 1.0, 2.0]])

plt.contour(xi, yi, zi, 15, linewidths=0.5, colors='k')
plt.contourf(xi, yi, zi, 15, cmap=plt.cm.jet)
plt.colorbar()
plt.show()

When using matplotlib with a contour plot, I'm having trouble getting the colorbar to display as I want. I've read through numerous similar examples, but have still not been able to get what I want.

In the image below, I want two things changed. I want the minimum value and maximum values to be display on the color bar (the max should be 2.0 and the min -0.1). These two values should be at the very edge of the colorbar. Also, I want the colorbar to display the value at every color transition. For example. in the plot below, between 2.1 and 1.8, there is another color transition where the value isn't displayed.

I think I may need to use norm, but it hasn't worked for me so far.

enter image description here

Code:

import numpy as np
import matplotlib.pyplot as plt

xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.array([[0., 1.0, 2.0],
               [0., 1.0, 2.0],
               [-0.1, 1.0, 2.0]])

plt.contour(xi, yi, zi, 15, linewidths=0.5, colors='k')
plt.contourf(xi, yi, zi, 15, cmap=plt.cm.jet)
plt.colorbar()
plt.show()

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

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

发布评论

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

评论(2

逆夏时光 2024-11-11 04:12:54

如果我正确理解你想要什么,我认为应该这样做:

import numpy as np
import matplotlib.pyplot as plt

xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.array([[0., 1.0, 2.0],
               [0., 1.0, 2.0],
               [-0.1, 1.0, 2.0]])

v = np.linspace(-.1, 2.0, 15, endpoint=True)
plt.contour(xi, yi, zi, v, linewidths=0.5, colors='k')
plt.contourf(xi, yi, zi, v, cmap=plt.cm.jet)
x = plt.colorbar(ticks=v)
print x
plt.show()

在此处输入图像描述

If I understand correctly what you want, I think this should do it:

import numpy as np
import matplotlib.pyplot as plt

xi = np.array([0., 0.5, 1.0])
yi = np.array([0., 0.5, 1.0])
zi = np.array([[0., 1.0, 2.0],
               [0., 1.0, 2.0],
               [-0.1, 1.0, 2.0]])

v = np.linspace(-.1, 2.0, 15, endpoint=True)
plt.contour(xi, yi, zi, v, linewidths=0.5, colors='k')
plt.contourf(xi, yi, zi, v, cmap=plt.cm.jet)
x = plt.colorbar(ticks=v)
print x
plt.show()

enter image description here

浮云落日 2024-11-11 04:12:54

替代方案:


    import numpy as np
    import matplotlib.pyplot as plt
    
    xi = np.array([0., 0.5, 1.0])
    yi = np.array([0., 0.5, 1.0])
    zi = np.array([[0., 1.0, 2.0],
                   [0., 1.0, 2.0],
                   [-0.1, 1.0, 2.0]])
    
    v = np.linspace(-.1, 2.0, 15, endpoint=True)
    
    fig, ax = plt.subplots()
    
    plt.contour(xi, yi, zi, v, linewidths=0.5, colors='k')
    ContourPlot = plt.contourf(xi, yi, zi, v, cmap=plt.cm.jet)
    
    ColorBar = fig.colorbar(ContourPlot) # Add a colorbar to a plot
    ColorBar.set_ticks(v)
    plt.show()

在此输入图像描述

Alternative:


    import numpy as np
    import matplotlib.pyplot as plt
    
    xi = np.array([0., 0.5, 1.0])
    yi = np.array([0., 0.5, 1.0])
    zi = np.array([[0., 1.0, 2.0],
                   [0., 1.0, 2.0],
                   [-0.1, 1.0, 2.0]])
    
    v = np.linspace(-.1, 2.0, 15, endpoint=True)
    
    fig, ax = plt.subplots()
    
    plt.contour(xi, yi, zi, v, linewidths=0.5, colors='k')
    ContourPlot = plt.contourf(xi, yi, zi, v, cmap=plt.cm.jet)
    
    ColorBar = fig.colorbar(ContourPlot) # Add a colorbar to a plot
    ColorBar.set_ticks(v)
    plt.show()

enter image description here

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