Python / Matplotlib - Colorbar范围和显示值

时间:2022-02-25 21:22:21

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.

当使用带有等高线图的matplotlib时,我无法按照我的意愿显示颜色条。我已经阅读了许多类似的例子,但仍然无法得到我想要的东西。

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.

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

Can anyone please help me? I think I may need to use norm, but it hasn't worked for me so far.

谁能帮帮我吗?我想我可能需要使用规范,但到目前为止它对我没用。

Thanks,

Python / Matplotlib  -  Colorbar范围和显示值

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()

1 个解决方案

#1


32  

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()

Python / Matplotlib  -  Colorbar范围和显示值

#1


32  

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()

Python / Matplotlib  -  Colorbar范围和显示值