matplotlib scatter edge without specifying edgecolor

后端 未结 1 1018
有刺的猬
有刺的猬 2020-12-03 22:24

It seems that now the default scatter plot marker is a filled circle without an edge. I want a marker with an edge and with facecolor=\"none\". But if facecolor=\"none\" but

相关标签:
1条回答
  • 2020-12-03 23:08

    There are two ways to produce empty or hollow scatter markers:

    Setting facecolor to "none"

    Instead of "just turning on" the edges, you may "turn off" the faces. So in order to make the facecolors of scatter markers transparent you may set the facecolor of the resulting PolyCollecton to "none".

    sc = ax.scatter(...)
    sc.set_facecolor("none")
    

    This is different from sc = ax.scatter(x,y, c=x, facecolor="none"), because the c argument overwrites the facecolor.

    Complete example:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0,2*np.pi,20)
    y = np.sin(x)
    
    fig, ax=plt.subplots()
    sc = ax.scatter(x,y, c=x, cmap="nipy_spectral")
    sc.set_facecolor("none")
    
    plt.show()
    

    Using non-filled marker

    A different option is to use a non-filled marker. This would have its facecolor only at the edge. An example may be marker="$\u25EF$" from the STIX font (also see this question)

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0,2*np.pi,20)
    y = np.sin(x)
    
    fig, ax=plt.subplots()
    
    sc = ax.scatter(x,y, c=x, marker="$\u25EF$", cmap="nipy_spectral")
    
    plt.show()
    

    Note: In python 2, you would need to use marker=ur"$\u25EF$" instead.

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