【问题标题】:setting color range in matplotlib patchcollection在 matplotlib patchcollection 中设置颜色范围
【发布时间】:2011-05-17 09:14:16
【问题描述】:

我正在 matplotlib 中绘制一个PatchCollection,其中包含从文件中读取的坐标和色块颜色值。

问题在于 matplotlib 似乎会自动将颜色范围缩放到数据值的最小值/最大值。如何手动设置颜色范围?例如。如果我的数据范围是 10-30,但我想将其缩放到 5-50 的颜色范围(例如与另一个图进行比较),我该怎么做?

我的绘图命令看起来与 api 示例代码中的相同:patch_collection.py

colors = 100 * pylab.rand(len(patches))
p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
p.set_array(pylab.array(colors))
ax.add_collection(p)
pylab.colorbar(p)

pylab.show()

【问题讨论】:

    标签: python matplotlib patch


    【解决方案1】:

    在您的示例中,使用p.set_clim([5, 50]) 设置颜色缩放最小值和最大值。 matplotlib 中任何有颜色图的东西都有get_climset_clim 方法。

    作为一个完整的例子:

    import matplotlib
    import matplotlib.pyplot as plt
    from matplotlib.collections import PatchCollection
    from matplotlib.patches import Circle
    import numpy as np
    
    # (modified from one of the matplotlib gallery examples)
    resolution = 50 # the number of vertices
    N = 100
    x       = np.random.random(N)
    y       = np.random.random(N)
    radii   = 0.1*np.random.random(N)
    patches = []
    for x1, y1, r in zip(x, y, radii):
        circle = Circle((x1, y1), r)
        patches.append(circle)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    colors = 100*np.random.random(N)
    p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
    p.set_array(colors)
    ax.add_collection(p)
    fig.colorbar(p)
    
    fig.show()
    

    现在,如果我们在调用fig.show(...) 之前在某处添加p.set_clim([5, 50])(其中p 是补丁集合),我们会得到:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多