gogo
gogo

Reputation: 239

Make colorbar compatible with gradient bar plot values

I want to make the values I plot to be compatible with the colorbar values. How can I do this? See more details below.

y1, y2, y3 values are : [-1.7 -1.62 -1.53 -1.43 -1.32 -1.2 -1.09 -0.97 -0.85], [-1.43 -1.28 -1.09 -0.88 -0.66 -0.44 -0.21 0.03 0.27], [-3.65 -3.58 -3.48 -3.38 -3.27 -3.16 -3.04 -2.92 -2.8 ]

import matplotlib.pyplot as plt
import numpy as np

#plot
fig = plt.figure(figsize=(9.6,6), dpi=300, linewidth=3.0)
ax = fig.add_subplot(311)
y1 = y.transpose()   #y should be the first data I gave out in the beginning
gradient = [ y1,y1 ]
plt.imshow(gradient, aspect='auto', cmap=plt.get_cmap('hot'))
ax.set_xticklabels(data[list[1]])

ax2 = fig.add_subplot(312)
y2 = y.transpose()  #y should be the second data I gave out in the beginning
gradient = [ y2,y2 ]
plt.imshow(gradient, aspect='auto', cmap=plt.get_cmap('hot'))
ax2.set_xticklabels(data[list[5]])

ax3 = fig.add_subplot(313)
y3 = y.transpose()  #y should be the third data I gave out in the beginning
gradient = [ y3,y3 ]
plt.imshow(gradient, aspect='auto', cmap=plt.get_cmap('hot'))
ax3.set_xticklabels(data[list[9]])

sm = plt.cm.ScalarMappable(cmap=plt.get_cmap('hot'),norm=plt.Normalize(-6.39,9.29))
sm._A = []
plt.colorbar(sm,ax=[ax,ax2,ax3])
#fig.set_tight_layout(True)       #how can I use this? If I use this it generates a warning and the plot overlaps
plt.savefig('CuxOxi.png',dpi=300,format='png', orientation='landscape')

enter image description here

As you can see from the graph, the colorbar ranges from -6.39 to 9.29. Each subplot ranges only a fraction of the complete colorbar range. How can I make for example -1.62 to -1.2 to have the color range as defined in the colorbar (which is mostly red)

Upvotes: 0

Views: 627

Answers (1)

tmwilson26
tmwilson26

Reputation: 791

In each plot, you can add the vmin and vmax options to the plt.imshow function in order to set the color scale min and max for that plot. You can define these to be the same for each plot so that they all have the same scale.

vmin = -6
vmax = 9

plt.imshow(gradient, aspect='auto', cmap=plt.get_cmap('hot'),vmin=vmin,vmax=vmax)

Upvotes: 2

Related Questions