Python Min and Max range for Color bar on Matplotlib Contour Graph

Fat*_*a90 3 python colors matplotlib contour colorbar

I am trying to edit the colorbar range on my contour graph from 0 to 0.12, I have tried a few things but it hasn't worked. I keep getting the full color bar range up until 0.3 which is not what I want.

This is my code:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
triang = tri.Triangulation(x, y)

plt.tricontour(x, y, z, 15, colors='k')

plt.tricontourf(x, y, z, 15, cmap='Blues', vmin=0, vmax=0.12,\
                extend ='both')
plt.colorbar()

plt.clim(0,0.12)

plt.ylim (0.5,350)

plt.xlim(-87.5,87.5)

plt.show()
Run Code Online (Sandbox Code Playgroud)

x, y, and z are all arrays with one column and large number of rows. You can take a look at my graph here:

在此处输入图片说明

Imp*_*est 5

我认为这个问题确实有效。@Fatma90:您需要提供一个工作示例,在您的情况下提供 x、y、z。

不管怎样,我们可以自己发明一些价值观。所以问题是, vmin 和 vmax 被简单地忽略了plt.tricontourf(),我不知道有什么好的解决方案。

但是这里有一个解决方法,手动设置 levels

plt.tricontourf(x, y, z, levels=np.linspace(0,0.12,11), cmap='Blues' )
Run Code Online (Sandbox Code Playgroud)

这里我们使用了 10 个不同的级别,看起来不错(如果使用不同数量的级别,问题可能是有漂亮的刻度线)。

我提供了一个工作示例来查看效果:

import numpy as np
import matplotlib.pyplot as plt

#random numbers for tricontourf plot
x = (np.random.ranf(100)-0.5)*2.
y = (np.random.ranf(100)-0.5)*2.
#uniform number grid for pcolor
X, Y = np.meshgrid(np.linspace(-1,1), np.linspace(-1,1))

z = lambda x,y : np.exp(-x**2 - y**2)*0.12

fig, ax = plt.subplots(2,1)

# tricontourf ignores the vmin, vmax, so we need to manually set the levels
# in this case we use 11-1=10 equally spaced levels.
im = ax[0].tricontourf(x, y, z(x,y), levels=np.linspace(0,0.12,11), cmap='Blues' )
# pcolor works as expected
im2 = ax[1].pcolor(z(X,Y), cmap='Blues', vmin=0, vmax=0.12 )

plt.colorbar(im, ax=ax[0])
plt.colorbar(im2, ax=ax[1])

for axis in ax:
    axis.set_yticks([])
    axis.set_xticks([])
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

这产生

在此处输入图片说明