4

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.

1 Answer 1

6

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( )
Sign up to request clarification or add additional context in comments.

1 Comment

This was the problem, thank you :) I moved the circle creation into my subplot loop and voilà, circles. Cheers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.