目录

背景

解决方法

第一处错误

解决方法

第二处错误 cm.coolwarm

解决方法

完整代码

使用其它颜色


背景

想了解一下 matplotlib 绘图,从官网 copy了以下示例代码,直接运行有两处报错,百度没有找到解决方法,无奈小白只能自己支棱起来

<原代码地址>

解决方法

  • 第一处错误

第23行的 ax.zaxis.set_major_formatter('{x:.02f}')

   原因:传入参数应该是 formatter 类型,不是字符串,现在代码里传入的是 '{x:.02f}'


  • 解决方法

从 matplotlib.ticker 导入 FormatStrFormatter

from matplotlib.ticker import LinearLocator, FormatStrFormatter

将第23行代码改为

ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

  • 第二处错误

第16行的  cmap=cm.coolwarm

原因:Cannot find reference 'coolwarm' in 'cm.py'   cm里找不到 coolwarm

也就是我们直接 cm.  根本找不到


  • 解决方法

 1.直接使用  cmap='coolwarm' 即可
 2.cmap=cm.get_cmap('coolwarm')
# 方法一
surf = ax.plot_surface(X, Y, Z, cmap='coolwarm', linewidth=0, antialiased=False)# 方法二
surf = ax.plot_surface(X, Y, Z, cmap=cm.get_cmap('spring'), linewidth=0, antialiased=False)

完整代码

import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as npfig, ax = plt.subplots(subplot_kw={"projection": "3d"})# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.get_cmap('spring'), linewidth=0, antialiased=False)
# 或
# surf = ax.plot_surface(X, Y, Z, cmap='rainbow', linewidth=0, antialiased=False)# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)plt.show()

效果 (颜色为 spring)


使用其它颜色

 使用 cm.cmap_d.keys() 获取支持的所有颜色
for color in cm.cmap_d.keys():print(color)-------------------------------------------------------------------------------------------
# 结果 164个magma
inferno
plasma
viridis
cividis
twilight
twilight_shifted
Blues
BrBG
BuGn
BuPu
CMRmap
GnBu
Greens
Greys
OrRd
Oranges
PRGn
PiYG
PuBu
PuBuGn
PuOr
PuRd
Purples
RdBu
RdGy
RdPu
RdYlBu
RdYlGn
Reds
Spectral
Wistia
YlGn
YlGnBu
YlOrBr
YlOrRd
afmhot
autumn
binary
bone
brg
bwr
cool
coolwarm
copper
cubehelix
flag
gist_earth
gist_gray
gist_heat
gist_ncar
gist_rainbow
gist_stern
gist_yarg
gnuplot
gnuplot2
gray
hot
hsv
jet
nipy_spectral
ocean
pink
prism
rainbow
seismic
spring
summer
terrain
winter
Accent
Dark2
Paired
Pastel1
Pastel2
Set1
Set2
Set3
tab10
tab20
tab20b
tab20c
magma_r
inferno_r
plasma_r
viridis_r
cividis_r
twilight_r
twilight_shifted_r
Blues_r
BrBG_r
BuGn_r
BuPu_r
CMRmap_r
GnBu_r
Greens_r
Greys_r
OrRd_r
Oranges_r
PRGn_r
PiYG_r
PuBu_r
PuBuGn_r
PuOr_r
PuRd_r
Purples_r
RdBu_r
RdGy_r
RdPu_r
RdYlBu_r
RdYlGn_r
Reds_r
Spectral_r
Wistia_r
YlGn_r
YlGnBu_r
YlOrBr_r
YlOrRd_r
afmhot_r
autumn_r
binary_r
bone_r
brg_r
bwr_r
cool_r
coolwarm_r
copper_r
cubehelix_r
flag_r
gist_earth_r
gist_gray_r
gist_heat_r
gist_ncar_r
gist_rainbow_r
gist_stern_r
gist_yarg_r
gnuplot_r
gnuplot2_r
gray_r
hot_r
hsv_r
jet_r
nipy_spectral_r
ocean_r
pink_r
prism_r
rainbow_r
seismic_r
spring_r
summer_r
terrain_r
winter_r
Accent_r
Dark2_r
Paired_r
Pastel1_r
Pastel2_r
Set1_r
Set2_r
Set3_r
tab10_r
tab20_r
tab20b_r
tab20c_r

matplotlib cm.coolwarm报错 解决方法相关推荐

  1. ThinkPad安装deepin操作系统报错解决方法

    ThinkPad安装deepin操作系统报错解决方法 参考文章: (1)ThinkPad安装deepin操作系统报错解决方法 (2)https://www.cnblogs.com/haihua85/p ...

  2. hive导出数据到本地文件报错解决方法

    hive导出数据到本地文件报错解决方法 参考文章: (1)hive导出数据到本地文件报错解决方法 (2)https://www.cnblogs.com/yaopeiyun/p/12232251.htm ...

  3. Failed to resolve: com.android.support:appcompat-v7:27.+ 报错解决方法

    Failed to resolve: com.android.support:appcompat-v7:27.+ 报错解决方法 参考文章: (1)Failed to resolve: com.andr ...

  4. AndroidStudio EventBus报错解决方法its super classes have no public methods with the @Subscribe

    AndroidStudio EventBus报错解决方法its super classes have no public methods with the @Subscribe 参考文章: (1)An ...

  5. @Data注解使用后get set报错解决方法

    @Data注解使用后get set报错解决方法 参考文章: (1)@Data注解使用后get set报错解决方法 (2)https://www.cnblogs.com/lijinshan950823/ ...

  6. 【PaddlePaddle系列】报错解决方法合集 (不定时更新)

    [PaddlePaddle系列]报错解决方法合集 (不定时更新) 参考文章: (1)[PaddlePaddle系列]报错解决方法合集 (不定时更新) (2)https://www.cnblogs.co ...

  7. vue 项目初始化时,npm run dev报错解决方法

    vue 项目初始化时,npm run dev报错解决方法 参考文章: (1)vue 项目初始化时,npm run dev报错解决方法 (2)https://www.cnblogs.com/ruilin ...

  8. Python 中文注释报错解决方法

    Python 中文注释报错解决方法 参考文章: (1)Python 中文注释报错解决方法 (2)https://www.cnblogs.com/cjiong/p/5881983.html 备忘一下.

  9. Redis 5.0.3默认配置启动报错解决方法

    Redis 5.0.3默认配置启动报错解决方法 参考文章: (1)Redis 5.0.3默认配置启动报错解决方法 (2)https://www.cnblogs.com/miclesvic/p/1031 ...

最新文章

  1. ViewPager撤消左右滑动切换功能
  2. 未来城市的无人机送货系统是怎样的?
  3. 1.4. Rosegarden
  4. 极客时间VIP年卡,任意专栏免费看,谁要?
  5. 更改eclipse的Package Explorer的字体
  6. java-web的mybatis的学习
  7. Flutter-Cookbook 非官方翻译
  8. artTemplate-3.0
  9. 一个好用的变换类TransformManager
  10. Windows-查看系统安装时间、启停某服务、组策略、限制外部设备命令
  11. 网络代理之后无法抓包的解决方案
  12. 主板检测卡c5_计算机主板检测卡检测显示C5是哪的問題?
  13. 注册测绘师-大地测量与平差-衡量精度的标准
  14. 评量子论的两本科普书籍
  15. 解决Page index must not be less than zero问题
  16. 《数字图像处理》-(3)-1从傅里叶级数到傅里叶变换详细推导以及傅里叶图像的性质
  17. VC 模拟鼠标和键盘输入
  18. 软件配置 | ios系统Clion下载、安装、配置环境
  19. 推特开发者账号 V2【推特开发者文档V2系列1】——获取推特视频播放量
  20. Py2neo安装(未完待续)

热门文章

  1. Altium Designer (AD)安装教程
  2. 一款针对百某盘的神器
  3. windows pipe
  4. less与sass的区别
  5. 443_J1939源地址与目标地址的使用和判断
  6. 模仿是学习的最佳途径
  7. 接口测试试题汇总jmeter
  8. java设计模式 (二) 创建模式
  9. CTF中的Crypto
  10. linux下生成ssh密钥并获取密钥