【问题标题】:How to interpret the documentation on LinearSegmentedColormap如何解释 LinearSegmentedColormap 上的文档
【发布时间】:2019-08-23 06:18:01
【问题描述】:

我使用我无法理解的 LinearSegmentedColormap 获得了这个示例代码。

type(cmap)matplotlib.colors.LinearSegmentedColormap

cmap.__dict__给了

 'name': 'jet',
 'N': 256,
 '_rgba_bad': (0.0, 0.0, 0.0, 0.0),
 '_rgba_under': None,
 '_rgba_over': None,
 '_i_under': 256,
 '_i_over': 257,
 '_i_bad': 258,
 '_isinit': False,
 'colorbar_extend': False,
 '_segmentdata': {'red': ((0.0, 0, 0),
   (0.35, 0, 0),
   (0.66, 1, 1),
   (0.89, 1, 1),
   (1, 0.5, 0.5)),
  'green': ((0.0, 0, 0),
   (0.125, 0, 0),
   (0.375, 1, 1),
   (0.64, 1, 1),
   (0.91, 0, 0),
   (1, 0, 0)),
  'blue': ((0.0, 0.5, 0.5),
   (0.11, 1, 1),
   (0.34, 1, 1),
   (0.65, 0, 0),
   (1, 0, 0))},
 '_gamma': 1.0}

LinearSegmentedColormap.__dict__ 给了

              '__doc__': '\n    Colormap objects based on lookup tables using linear segments.\n\n    The lookup table is generated using linear interpolation for each\n    primary color, with the 0-1 domain divided into any number of\n    segments.\n    ',
              '__init__': <function matplotlib.colors.LinearSegmentedColormap.__init__(self, name, segmentdata, N=256, gamma=1.0)>,
              '_init': <function matplotlib.colors.LinearSegmentedColormap._init(self)>,
              'set_gamma': <function matplotlib.colors.LinearSegmentedColormap.set_gamma(self, gamma)>,
              'from_list': <staticmethod at 0x1bc1ae2b5c0>,
              '_resample': <function matplotlib.colors.LinearSegmentedColormap._resample(self, lutsize)>,
              'reversed': <function matplotlib.colors.LinearSegmentedColormap.reversed(self, name=None)>})

cmap 不是LinearSegmentedColormap 的实例吗?在这种情况下,为什么它的属性和功能与LinearSegmentedColormap 类中的属性和功能不对应?

另一个问题是行时发生了什么:

colors = cmap(np.arange(cmap.N))

运行了吗?我想其中一个类方法正在运行,但是哪个?是否有可能从LinearSegmentedColormap 的文档中找出发生了什么?

from matplotlib.colors import LinearSegmentedColormap  
import matplotlib as plt  
import numpy as np  

cmap = plt.cm.get_cmap('jet')  
type(cmap)  
cmap.**__dict__**  
colors = cmap(np.arange(cmap.N))  

LinearSegmentedColormap.__dict__ 

【问题讨论】:

    标签: python-3.x matplotlib


    【解决方案1】:

    __dict__ 类和实例__dict__ 之间的区别问题可能已经在instance.__dict__ & class.__dict__dict in Python object instance doesn't contain the methods of the class 中得到解答。在任何情况下它们都是不同的,并且实例__dict__ 不包含所有类属性。

    问题的核心似乎是找出如果调用类实例会发生什么。

    您可以调用任何具有__call__ 方法的实例。对于颜色图,LinearSegmentedColormap 是基础Colormap 的子类。 Colormap 有一个 __call__ 方法。它接受一个(列表)数字并返回一个(列表)颜色。

    是否有可能从 LinearSegmentedColormap 的文档中找出发生了什么?

    不幸的是,情况似乎并非如此。在matplotlib.colors 文档以及每个Colormap 子类中可能应该有一句话解释它。
    欢迎对文档进行改进。

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-15
      • 2013-06-12
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      相关资源
      最近更新 更多