为什么我的颜色栏不显示颜色?

时间:2020-02-07 12:15:52

标签: matplotlib

肯定我缺少明显的东西,但我不太喜欢Matplotlib在本示例结尾为我创建的颜色栏

import matplotlib.pyplot as plt 
import numpy as np

x = np.arange(50)
xy = x*x[:,None]/200 # 0.000 to 12.005

plt.contourf(xy, levels=100)
plt.contour(xy,levels=12, colors='black').clabel(inline=1, fmt='%.2f')
plt.colorbar()

colorbars should be colored

1 个答案:

答案 0 :(得分:4)

请尝试更改轮廓的顺序,因为plt将捕获最新的对象,并且具有12个级别的轮廓没有颜色:

import matplotlib.pyplot as plt 
import numpy as np

x = np.arange(50)
xy = x*x[:,None]/200 # 0.000 to 12.005

plt.contour(xy, levels=12, colors='black').clabel(inline=1, fmt='%.2f')
plt.contourf(xy, levels=100)
plt.colorbar()

或:,您可以将所需的轮廓直接传递到plt.colobar()

...
c = plt.contourf(xy, levels=100)
plt.contour(xy, levels=12, colors='black').clabel(inline=1, fmt='%.2f')
plt.colorbar(c)

img

具有相同级别值:

...
plt.contour(xy, levels=12, colors='black').clabel(inline=1, fmt='%.2f')
plt.contourf(xy, levels=12)
plt.colorbar()

img2