Home > Software design >  Altering the middle point of the colors assigned to a scatterplot using a colorbar in matplotlib
Altering the middle point of the colors assigned to a scatterplot using a colorbar in matplotlib

Time:12-09

I have a simple scatter plot with a colorbar on the points as follows:

import pandas as pd
import matplotlib.pyplot as plt

col1, col2, col3 = [], [], []
for i in range(0,21):
    col1.append(i)
    col2.append(i**1.5)
    col3.append(i)
    
data = pd.DataFrame({'col1': col1, 'col2': col2, 'col3': col3})

fig = plt.figure()
ax = plt.axes()
im = ax.scatter(data['col1'], data['col2'], c=data['col3'], cmap='RdBu')
cbar = fig.colorbar(im, ax=ax)
plt.show()

The colorbar here automatically takes 10 as the middle point as the data of my c ranges from 0 to 20 and colors my points in the scatter plot accordingly, is there a way to assign the middle point of the colorbar yourself? For instance, making the middle point 5 rather than 10 in this example this way the white color would be assigned to 5 and the colorbar would still reach from 0 to 20.

CodePudding user response:

You can pass coustom enter image description here

  • Related