Matplotlib - 设置纵横比时的轴碰撞警告

Matplotlib - Axes collision warning when setting aspect ratio

我正在使用 matplotlib 来绘制 hexbin。举个简单的例子-

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()

我收到以下警告-

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

我认为是因为这条线-

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

在这种情况下如何使用不同的参数。 matplotlib的版本是2.1.1

您似乎并不想创建新轴。所以不要在这里使用 plt.axes() 。相反,get c当前的 ax 以通常的方式 (plt.gca()) 和使用它的任何方法。

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