Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - Axes collision warning when setting aspect ratio

I am using matplotlib to plot a hexbin. As a simple example-

import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(100)
y = np.random.rand(100)

plt.hexbin(x, y, gridsize = 15, cmap='inferno')

plt.gca().invert_yaxis() # To make top left corner as origin

plt.axes().set_aspect('equal', 'datalim')
plt.show()

I get the following warning-

"MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance."

I think it is due to the line-

plt.axes().set_aspect('equal', 'datalim')

How can I use different arguments in this case. The version of matplotlibis 2.1.1

like image 690
amadispstac Avatar asked Jul 18 '26 12:07

amadispstac


1 Answers

It doesn't seem like you want to create a new axes anyways. So don't use plt.axes() here. Instead get the current axes in the usual way (plt.gca()) and use any of its methods.

plt.gca().set_aspect('equal', 'datalim')
like image 81
ImportanceOfBeingErnest Avatar answered Jul 20 '26 03:07

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!