Skip to content Skip to sidebar Skip to footer

Change Colour Of Colorbar In Python Matplotlib

I have a code that gives me a scatter plot of predicted vs actual values as a function of concentration. The data is pulled from an excel csv spreadsheet. This is the code: import

Solution 1:

To get the right color bar, use the following code:

colormap = plt.cm.get_cmap('plasma') # 'plasma' or 'viridis'
colors = colormap(scaled_z)
sc = plt.scatter(x, y, c=colors)
sm = plt.cm.ScalarMappable(cmap=colormap)
sm.set_clim(vmin=0, vmax=100)
plt.colorbar(sm)
plt.xlabel("Actual")
plt.ylabel("Predicted")
plt.show()

For my random generated data I got the following plot:

enter image description here

Now replace 'plasma' with 'viridis' and check the other variant.

Solution 2:

Your code return for me an error TypeError: You must first set_array for mappable ...

The following is a simplest syntax that works for me:

import matplotlib.pyplot as plt
import numpy as np

a = np.random.random(100)
b = np.random.random(100)

scaled_z = (a + b)/a

plt.figure()
plt.scatter(a, b, c = scaled_z, cmap = 'plasma') ## you can directly change the colormap here
plt.colorbar()
plt.tight_layout()
plt.show()

Solution 3:

You should not scale your data, unless you want the colorbar to be incorrect. Once you have the PathCollection from the scatter call, you can call set_cmap and set_clim on that and the colorbar should track. (you could also explicitly associate the colorbar with the PathCollection to avoid ambiguity)

import matplotlib.pyplot as plt
import numpy as np


x = np.random.randn(100)
y = np.random.randn(100)
z = np.random.randn(100)

sc=plt.scatter(x, y, c=z, cmap='viridis')
plt.clim(0, 100)
plt.colorbar(sc)
plt.xlabel("Actual")
plt.ylabel("Predicted")
sc.set_cmap('plasma')
sc.set_clim(-1, 1)

plt.show()

enter image description here

Post a Comment for "Change Colour Of Colorbar In Python Matplotlib"