qq_43268506 2023-02-14 17:42 采纳率: 50%
浏览 17
已结题

学习matplotlib遇到的问题,cm模块没定义的东西还能用,为什么

matplotlib里面的写法千奇百怪啊
散点图scatter方法的渐变色


import matplotlib.pyplot as plt

x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]

# plt.cm.Blues 渐变为蓝色,y值越大颜色越深
plt.scatter(x_values, y_values, s=40, edgecolor='none', c=y_values, cmap=plt.cm.Blues)

我想问的是,cm文件里没有Blues属性啊,但是却能用,这是什么原因

Blues找不到定义

img

……

附上cm的结构

img

运行代码结果
……
散点图

img

……
Python编程是不是很难啊,我感觉有很多内容藏着掖着,什么原理都搞不清啊……

  • 写回答

2条回答 默认 最新

  • GoodCoder666 2023-02-14 17:51
    关注

    代码能运行,说明Blues这个属性肯定是存在的。但是它可以被动态添加(编辑器只能找到静态属性),比如下面的例子:

    # 动态添加变量s,相当于 s = 'Hello world'
    globals()['s'] = 'Hello world'
    
    # 输出
    print(s)
    

    这里的print(s),编辑器也会报错说不存在变量,但实际上这段代码是可以运行的。

    另外,回到问题,Blues属性的数据定义在隐藏的依赖_cm.py

    # 34 colormaps based on color specifications and designs
    # developed by Cynthia Brewer (https://colorbrewer2.org/).
    # The ColorBrewer palettes have been included under the terms
    # of an Apache-stype license (for details, see the file
    # LICENSE_COLORBREWER in the license directory of the matplotlib
    # source distribution).
    
    # RGB values taken from Brewer's Excel sheet, divided by 255
    
    _Blues_data = (
        (0.96862745098039216,  0.98431372549019602,  1.0                ),
        (0.87058823529411766,  0.92156862745098034,  0.96862745098039216),
        (0.77647058823529413,  0.85882352941176465,  0.93725490196078431),
        (0.61960784313725492,  0.792156862745098  ,  0.88235294117647056),
        (0.41960784313725491,  0.68235294117647061,  0.83921568627450982),
        (0.25882352941176473,  0.5725490196078431 ,  0.77647058823529413),
        (0.12941176470588237,  0.44313725490196076,  0.70980392156862748),
        (0.03137254901960784,  0.31764705882352939,  0.61176470588235299),
        (0.03137254901960784,  0.18823529411764706,  0.41960784313725491)
        )
    

    望采纳,谢谢。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月22日
  • 已采纳回答 2月14日
  • 创建了问题 2月14日

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器