我有以下奇怪的行为:当我限制图形的范围时,颜色图仍然显示它:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
x = np.linspace(0,1,100)
X,Y = np.meshgrid(x,x,indexing="ij")
im = ax.contourf(X,Y,X**2-Y**2, 100, vmin = 0, vmax = 0.5)
plt.colorbar(im, ax=ax)
plt.show()
![enter image description here](https://i.stack.imgur.com/96Shd.png)
如何正确配置颜色条的限制?
100以内的ax.contourf()
表示您希望轮廓内有 100 个级别。绘图本身中确实有超过 0.5 的值。
您可以自定义颜色条刻度的范围。
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
cbarticks = np.arange(0.0,0.55,0.05)
x = np.linspace(0,1,100)
X,Y = np.meshgrid(x,x,indexing="ij")
im = ax.contourf(X,Y,X**2-Y**2, cbarticks, vmin = 0, vmax = 0.5)
plt.colorbar(im, ax=ax,ticks=cbarticks)
plt.show()
这会给你
![enter image description here](https://i.stack.imgur.com/0FSbF.png)
不确定这是否正是您想要的,但我有一个类似的问题并自己在这里回答:颜色条问题
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)