Ste*_*eve 4 python geometry matplotlib subplot
有点奇怪,我显然错过了一些东西,但我得到了一些非常奇怪的行为,而且我无法弄清楚我做错了什么。
我有一个带有网格格式子图的图(为了这篇文章,我只说一个 2 × 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( )
Run Code Online (Sandbox Code Playgroud)
输出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( )
Run Code Online (Sandbox Code Playgroud)
输出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( )
Run Code Online (Sandbox Code Playgroud)
我真的不明白这种行为(为什么示例 2 有效,但示例 1 或 3 不起作用?),或者我正在做什么导致它发生。任何人都可以透露一些信息吗?提前致谢。
rdR*_*ul 6
您对两个不同的补丁使用相同的“圆形”图,我认为这会产生问题,它会引发错误
无法重置轴。您可能正在尝试在多个不受支持的轴中重复使用一位艺术家
您需要为每个子图创建不同的圆圈,
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( )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7192 次 |
| 最近记录: |