python - Python / Matplotlib:带有contourf的颜色条不尊重自定义cmap的刻度标签
问题描述
我创建了一个自定义 cmap 和 ticklabels 以使用 contourf 绘制绘图,但并不是所有的 ticklabels 或所有颜色都被颜色条考虑,但是当我使用 imshow 时,我得到了我想要的结果。这是我的代码。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
from matplotlib.colors import BoundaryNorm
x = np.arange(-6,6,0.25)
y = np.arange(-6,6,0.25)
x, y = np.meshgrid(x,y)
z = np.sqrt(x**2+y**2)
newcolors = np.vstack((plt.cm.YlGn(np.linspace(0, 1, 4))[1:,:], plt.cm.Blues(np.linspace(0, 1, 6))))
palette = ListedColormap(newcolors, name='test')
palette.set_over('darkred')
palette.set_under('yellow')
tickslabels=[0.5,1.0,1.5,2.0,4.0,6.0,8.0,10.0,12.0,14.0]
norm=BoundaryNorm(tickslabels, len(tickslabels)-1)
fig1 = plt.figure('imshow')
img=plt.imshow(z, cmap=palette, norm=norm)
plt.colorbar(img, ticks=tickslabels, spacing='proportional', extend='both')
plt.title('imshow')
fig2 = plt.figure('contourf')
img=plt.contourf(x, y, z, cmap=palette, levels=tickslabels, extend='both') #norm=norm)
plt.colorbar(img, ticks=tickslabels, spacing='proportional', extend='both')
plt.title('contourf')
plt.show()
这是使用 imshow 和 contourf 的结果。注意imshow的colorbar,绿色从0.5到2.0,蓝色从2.0到14.0,这就是我想要的结果。然而使用contourf的结果是不一样的。我的错误是什么?我忘了设置任何参数?
解决方案
推荐阅读
- java - Kafka 多主题输入转换为多输出主题
- pytorch - RuntimeError: symeig_cuda: 算法收敛失败
- python - 图像时刻 - 打开 CV 质心计算
- vb.net - 如何在使用列表和字典之间获得第一个最快的出现
- python - 使用请求库为 HTTP 请求设置 DNS 超时
- c# - 如何使用 QBFC 从现有 Quickbooks 发票更新或修改自定义模板字段
- sql - 有没有办法比较sql中句子之间的相似度?
- c++ - 如何正确混合 OpenGL 中从步行动画到跑步动画的骨骼动画?
- java - 空指针指向方法 getItemCount。我应该初始化那个方法吗?
- fastlane - Fastlane:使用设备更新更新匹配配置文件