python - Matplotlib:2 x 1 的最简单示例 - 绘图网格因“无法创建对布尔的弱引用”而失败
问题描述
我正在尝试使用 matplotlib 做最简单的 1 行 2 列多轴绘图,实际上取自文档中的示例:
from matplotlib import pyplot as plt
fig, (ax1, ax2) = plt.subplot(nrows=1, ncols=2, sharey=True)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-4bc4f1ccd8e8> in <module>
1 from matplotlib import pyplot as plt
----> 2 fig, (ax1, ax2) = plt.subplot(nrows=1, ncols=2, sharey=True)
~/.virtualenvs/tcpsp/lib/python3.7/site-packages/matplotlib/pyplot.py in subplot(*args, **kwargs)
1082
1083 fig = gcf()
-> 1084 a = fig.add_subplot(*args, **kwargs)
1085 bbox = a.bbox
1086 byebye = []
~/.virtualenvs/tcpsp/lib/python3.7/site-packages/matplotlib/figure.py in add_subplot(self, *args, **kwargs)
1365 self._axstack.remove(ax)
1366
-> 1367 a = subplot_class_factory(projection_class)(self, *args, **kwargs)
1368 self._axstack.add(key, a)
1369 self.sca(a)
~/.virtualenvs/tcpsp/lib/python3.7/site-packages/matplotlib/axes/_subplots.py in __init__(self, fig, *args, **kwargs)
68
69 # _axes_class is set in the subplot_class_factory
---> 70 self._axes_class.__init__(self, fig, self.figbox, **kwargs)
71 # add a layout box to this, for both the full axis, and the poss
72 # of the axis. We need both because the axes may become smaller
~/.virtualenvs/tcpsp/lib/python3.7/site-packages/matplotlib/axes/_base.py in __init__(self, fig, rect, facecolor, frameon, sharex, sharey, label, xscale, yscale, **kwargs)
471 self._shared_x_axes.join(self, sharex)
472 if sharey is not None:
--> 473 self._shared_y_axes.join(self, sharey)
474 self.set_label(label)
475 self.set_figure(fig)
~/.virtualenvs/tcpsp/lib/python3.7/site-packages/matplotlib/cbook/__init__.py in join(self, a, *args)
923
924 for arg in args:
--> 925 set_b = mapping.get(weakref.ref(arg), [weakref.ref(arg)])
926 if set_b is not set_a:
927 if len(set_b) > len(set_a):
TypeError: cannot create weak reference to 'bool' object
<Figure size 432x288 with 0 Axes>
老实说,我不知道为什么它会尝试创建对 a 的弱引用bool
。
我正在使用最新版本的 matplotlib 和 Python:
import sys
print(sys.version)
3.7.0 (default, Sep 12 2018, 18:30:08)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
import matplotlib
matplotlib.__version__
'3.0.2'
我是否可以假设文档中的示例应该有效?这是 Matplotlib 错误还是我做错了什么?
解决方案
首先,使用plt.subplots()
在您想要单独自定义每个图之前,不解压您的 ax subplots 变量可能会更容易。
通常我会怎么做:
fig, ax = plt.subplots(1, 2, dpi=175)
quadrants = 0, 1
所以如果我需要在全球范围内应用任何东西,我可以这样做:
for quad in quadrants:
ax[quad]. (whatever)
然后分别:
ax[0].plot(...)
这也可以很好地扩展,因此如果我使用的是 2x2 的图矩阵(就像我现在一样),区别很简单:
fig, ax = plt.subplots(2, 2, dpi=175)
quadrants = (0,0),(0,1),(1,0),(1,1)
推荐阅读
- vue.js - 尝试创建 Vue 项目时如何修复版本不匹配错误?
- spacy - Spacy没有标记周期
- git - 开发分支中的标记功能
- python - 将服务器 ID 的部分发送到它旁边
- spring-boot - 如何为特定的 Rest Controller 禁用 Spring Boot 身份验证控制
- reactjs - 当我在项目中的任何地方都看不到引导程序时,我的 React 应用程序如何使用引导程序类名称?
- flutter - 是否可以将数据存储在某种模型中?
- javascript - JAWS 可访问性问题:选项标签的 aria-label 属性不起作用
- c# - Windows 窗体始终在主监视器上启动
- python - 试图运行一个python脚本,我主要在我的gpu上使用opencv