Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to close gap between python contour plot and colorbar

I have made a contour plot and set plt.axis = ‘square’. This has caused a significant gap between the plot and the color bar. I have been struggling to find a way to close this gap. How can you choose the distance between a contour plot and its color bar? How can you close this gap up to the point that they are touching?

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
data = [[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10]]
plt.contourf(data)
plt.colorbar()
plt.xlabel('y label')
plt.ylabel('x label')
plt.axis('square')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can simply use the pad attribute of plt.colorbar, and set it to -0.2 to align it with the plot itself. In case you want the colorbar bigger/smaller, you can also use the shrink attribute. More info here.

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

data = [[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10]]
plt.contourf(data)
plt.colorbar(pad = -0.2)

plt.xlabel('y label')
plt.ylabel('x label')
plt.axis('square')

Output:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading