Search code examples
pythonmatplotlibcolorbar

Re: Add colorbar to scatterplot with non-filled circles


I know this question has been asked and answered twice before, but those solutions are >4 yrs old now, and don't work for me at least. When I try out the this code:

import matplotlib.pyplot as plt
x = np.random.random(size=(100,))
y = np.random.random(size=(100,))
c = np.random.random(size=(100,))
fig, ax = plt.subplots()
g = ax.scatter(x, y, marker = 'o',c = c)
g.set_facecolor('none')
fig.colorbar(g)

I get an empty plot like:

screenshot

Has anyone else encountered this, and maybe found a solution? I'm using matplotlib 3.5.1.


Solution

  • I'm having the same issue, but by using a non-filled marker you should get what you want:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.random.random(size=(100,))
    y = np.random.random(size=(100,))
    c = np.random.random(size=(100,))
    fig, ax = plt.subplots()
    g = ax.scatter(x, y, marker="$\u25EF$", c=c, s=100)
    fig.colorbar(g)
    

    enter image description here

    For further details, look at this answer.