我有一個代碼(以下示爲最小的工作實施例,MWE
)繪製彩條時會產生一個警告:捕捉matplotlib警告
/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py:1533: UserWarning: This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.
warnings.warn("This figure includes Axes that are not "
欲捕獲這個警告,因此不顯示它。
我知道我應該沿着How do I catch a numpy warning like it's an exception (not just for testing)?這個問題陳述的內容應用一些東西,但我不知道該怎麼做。
這裏的MWE
:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
x = np.random.randn(60)
y = np.random.randn(60)
z = [np.random.random() for _ in range(60)]
fig = plt.figure()
gs = gridspec.GridSpec(1, 2)
ax0 = plt.subplot(gs[0, 0])
plt.scatter(x, y, s=20)
ax1 = plt.subplot(gs[0, 1])
cm = plt.cm.get_cmap('RdYlBu_r')
plt.scatter(x, y, s=20 ,c=z, cmap=cm)
cbaxes = fig.add_axes([0.6, 0.12, 0.1, 0.02])
plt.colorbar(cax=cbaxes, ticks=[0.,1], orientation='horizontal')
fig.tight_layout()
plt.show()
這正是我最終使用brycepg,謝謝你添加你的答案! – Gabriel