绘图库;向子图添加圆圈 - Issue/Confused

Matplotlib; adding circle to subplot - Issue/Confused

有点奇怪,我显然遗漏了一些东西,但我遇到了一些非常奇怪的行为,我无法弄清楚我做错了什么。

我有一个带有子图的网格格式的图(为了这个 post,我会说只是一个 2 x 2 网格)。我想在每个上绘制一些东西并添加一个圆圈。应该很容易,但它并不像我预期的那样。

示例代码 1:

import matplotlib.pyplot as plt

x = [ -1.0, -0.5, 0.0, 0.5, 1.0 ]
y = [  0.7,  0.2, 1.0, 0.0, 0.0 ]

circle = plt.Circle( ( 0, 0 ), 1 )

fig, axes = plt.subplots( 2, 2 )

axes[ 0, 0 ].plot( x, y )
axes[ 1, 1 ].plot( x, y )

axes[ 0, 0 ].add_patch( circle )
axes[ 1, 1 ].add_patch( circle )

plt.show( )

输出 1:

示例代码 2:

import matplotlib.pyplot as plt

x = [ -1.0, -0.5, 0.0, 0.5, 1.0 ]
y = [  0.7,  0.2, 1.0, 0.0, 0.0 ]

circle = plt.Circle( ( 0, 0 ), 1 )

fig, axes = plt.subplots( 2, 2 )

axes[ 0, 0 ].plot( x, y )
axes[ 1, 1 ].plot( x, y )

axes[ 0, 0 ].add_patch( circle )
#axes[ 1, 1 ].add_patch( circle )

plt.show( )

输出 2:

示例代码 3:

import matplotlib.pyplot as plt

x = [ -1.0, -0.5, 0.0, 0.5, 1.0 ]
y = [  0.7,  0.2, 1.0, 0.0, 0.0 ]

circle = plt.Circle( ( 0, 0 ), 1 )

fig, axes = plt.subplots( 2, 2 )

axes[ 0, 0 ].plot( x, y )
axes[ 1, 1 ].plot( x, y )

#axes[ 0, 0 ].add_patch( circle )
axes[ 1, 1 ].add_patch( circle )

plt.show( )

输出 3:

我真的不明白这种行为(为什么示例 2 有效但 1 或 3 无效?),或者我正在做什么导致它。任何人都可以阐明一下吗?提前致谢。

你对两个不同的补丁使用了相同的 'circle' 图,我认为这会产生问题,它会引发错误

Can not reset the axes. You are probably trying to re-use an artist in more than one Axes which is not supported

您需要为每个子图创建不同的圆圈,

import matplotlib.pyplot as plt

x = [ -1.0, -0.5, 0.0, 0.5, 1.0 ]
y = [  0.7,  0.2, 1.0, 0.0, 0.0 ]

circle1 = plt.Circle( ( 0, 0 ), 1 )
circle2 = plt.Circle( ( 0, 0 ), 1 )

fig, axes = plt.subplots( 2, 2 )

axes[ 0, 0 ].plot( x, y )
axes[ 1, 1 ].plot( x, y )

axes[ 0, 0 ].add_patch( circle1 )
axes[ 1, 1 ].add_patch( circle2 )

plt.show( )