setting color range in matplotlib patchcollection

前端 未结 1 462
情歌与酒
情歌与酒 2021-02-04 13:10

I am plotting a PatchCollection in matplotlib with coords and patch color values read in from a file.

The problem is that matplotlib seems to automatically scale the co

相关标签:
1条回答
  • 2021-02-04 13:39

    Use p.set_clim([5, 50]) to set the color scaling minimums and maximums in the case of your example. Anything in matplotlib that has a colormap has the get_clim and set_clim methods.

    As a full example:

    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)
    plt.colorbar(p)
    
    plt.show()
    

    enter image description here

    Now, if we just add p.set_clim([5, 50]) (where p is the patch collection) somewhere before we call plt.show(...), we get this: enter image description here

    0 讨论(0)
提交回复
热议问题