Python / Matplotlib-颜色栏范围和显示值 [英] Python/Matplotlib - Colorbar Range and Display Values

查看:128
本文介绍了Python / Matplotlib-颜色栏范围和显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将matplotlib与等高线图配合使用时,我很难使颜色栏显示为所需的颜色。我已经阅读了许多类似的示例,但是仍然无法获得我想要的。

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.

在下图中,我希望更改两件事。我希望最小值和最大值显示在颜色栏上(最大值应为2.0,最小值应为-0.1)。这两个值应位于颜色栏的最边缘。另外,我希望颜色栏在每次颜色转换时都显示该值。例如。在下面的图中,从2.1到1.8,还有另一个颜色过渡,其中的值未显示。

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.

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

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

谢谢

代码:

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


推荐答案

如果我正确理解了您想要的内容,我认为应该这样做

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-颜色栏范围和显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆