adplus-dvertising

Not able to generate the correct graph in Python

Asked 2 months ago
Viewed 26 times

So I have been trying to generate a heatmap in python such that the Matplotlib graph that is generated does not have any white margins in both x and y axes and the graph's scaling must be 1:1 with respect to the x and y axes units. I have tried many things but the best I have been able to achieve so far is able to remove the x axis white margins but not the y one. Here's an example: This is what I am getting: This is what I am getting This is what I want: This is what I want

Here's the code through which I am generating the graph:

plt.style.use('classic')
plt.axis('equal')
plt.pcolormesh(x_mesh,y_mesh,intensity)
plt.plot(x,y,linestyle="None",marker='.',markerfacecolor='white')
#plt.colorbar()
#plt.axis('off')
plt.margins(x=0,y=0)
plt.ylim([0,254])
plt.xlim([0,538])
plt.savefig('plottest.png', dpi=144, bbox_inches='tight', pad_inches=0)
plt.show()

Any help would be highly appreciated! Thanks!

asked 2 months ago

Correct Answer

Try changing plt.axis('equal') to plt.axis('scaled'). As the documentation says:

equal: ... Explicit data limits may not be respected in this case.

answered 2 months ago