Matplotlib:2 x 1 的最简单示例 - 绘图网格失败 "cannot create weak reference to bool"
Matplotlib: Simplest possible example for 2 x 1 - grid of plots fails with "cannot create weak reference to bool"
我正在尝试使用 matplotlib 绘制最简单的 1 行 2 列多轴图,实际上 taken from the examples in the documentation:
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>
老实说,我不知道为什么它会尝试创建对 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 子图变量可能更容易。
通常我会怎么做:
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)
我正在尝试使用 matplotlib 绘制最简单的 1 行 2 列多轴图,实际上 taken from the examples in the documentation:
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>
老实说,我不知道为什么它会尝试创建对 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 子图变量可能更容易。
通常我会怎么做:
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)