【发布时间】:2014-03-06 14:18:39
【问题描述】:
我有一个代码(在下面显示为一个最小的工作示例,MWE),它在绘制颜色条时会产生警告:
/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()
【问题讨论】:
标签: python matplotlib warnings