Search code examples
pythonmatplotlibscatter-plotcolorbar

Colorbar scale with matplotlib


I'm trying to plot the price according to the longitude and latitude of the house. I want to add a colorbar, vmin=75000 and vmax=7700000, but the scale with my code is from 1 to 1000000. How do I change that please?

My code:

fig, ax1 = plt.subplots()
fig.set_size_inches(16,10)

cm = plt.cm.get_cmap('RdYlBu')

# On crée ensuite notre figure
im= ax1.scatter(df["long"], df["lat"], c=df["price"], s=1, vmin=75000, vmax=7700000, cmap=cm)
fig.colorbar(im, ax=ax1)

# on ajoute les titres
ax1.set_title("Prix en fonction de la latittude et longitude")
ax1.set_xlabel("longitude")
ax1.set_ylabel("latitude")

Solution

  • One way to do this is to specify ticks for the color bar. You can change the colorbar line like this...

    ColorBar=fig.colorbar(im, ax=ax1)
    ColorBar.set_ticks(np.linspace(75000, 7700000, 10, endpoint=True))
    

    This will set the ticks at start of 7500, end at 7700000 and provide 10 ticks...

    The colorbar should look something like this...

    enter image description here