Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib; adding circle to subplot - Issue/Confused

Bit of an odd one, and I'm clearly missing something, but I'm getting some really weird behaviour, and I can't work out what I'm doing wrong.

I have a plot with subplots in a grid format (for the sake of this post, I'll say just a 2 by 2 grid). I want to plot some stuff on each and also add a circle. Should be easy, but it's not acting as I expect.

Example Code 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( )

Output 1:

Output 1

Example Code 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( )

Output 2:

Output 2

Example Code 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( )

Output 3:
Output 3

I really don't understand this behaviour (why does example 2 work but not 1 or 3?), or what I'm doing to cause it. Can anyone shed some light? Thanks in advance.

like image 518
Steve Avatar asked Oct 25 '25 21:10

Steve


1 Answers

you are using same 'circle' plot for two different patches i think that is creating problem,it throws an error

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

you need to create different circles for each of the the subplots,

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( )
like image 172
rdRahul Avatar answered Oct 28 '25 10:10

rdRahul