Search code examples
pythonmatplotlibcolor-mappingprettyplotlib

Reduce Whitespace in pcolor matplotlib plot


I'm currently working on plotting pivoted tables using matplotlib pcolor but my axes are not functioning like i would like them to. I plot the tables using:

pyl.pcolor(pivot_99)
pyl.colorbar()
pyp.suptitle('Pivot 99', fontsize=14)
pyp.xlabel('Site Number', fontsize=12)
pyp.ylabel('Site Number', fontsize=12)
pyp.yticks(arange(len(pivot_99.index)),pivot_99.index)
pyp.xticks(arange(len(pivot_99.columns)),pivot_99.columns, rotation = 90)
pyl.savefig('Pivot 99.png')
pyl.show()

Where pivot_99 is a dataframe consisting of numbers between 0 and 1. The length of the index and columns are always the same but are different values for different years. When I plot pivot_99 it looks fine: Pivot 1999

However when I plot the 2000 Heatmap there is extra space on the top and right.

Pivot 2000

Does anyone know why this is happening or how I can fix the graph so only space with color is showing? Thanks


Solution

  • Try with set_xlim and set_ylim like in this example:

    import matplotlib.pyplot as pyp
    
    pyp.gca().set_xlim((1,10))
    pyp.gca().set_ylim((1,12))
    pyp.plot([1,2],[1,2])
    pyp.show()
    

    You can try with xlim=ylim=558, I can test it because I don't have your data :(