0

How do I set the axes range after I create an figure? I want create a 2 inch by 1 inch figure with three circles next to each other. Currently I am trying to play around with the following a,b,c,d with no success

a=0
b=0
c=5
d=10

sradius = .5

fig = plt.figure(num=0, figsize=[2,1], dpi=300, facecolor = 'w', edgecolor = 'k', frameon=True)
ax = fig.add_axes([a,b,c,d])

states = [plt.Circle((sradius+x,sradius), radius=sradius, fc='y') for x in range(4)]

for state in states: ax.add_patch(state)

fig.show()

Output figure

What I want is a 2 inch by 1 inch figure where the y axis goes from 0 to 5 and the x axis goes from 0 to 10. Changing a,b,c,d always keeps the axes going from 0 to 1. What do I do?

Shep Bryan
  • 567
  • 5
  • 13

2 Answers2

0

you can use plt.xlim() and plt.ylim() to control the axis limits.

e.g.

a=0
b=0
c=5
d=10

sradius = .5

fig = plt.figure(num=0, figsize=[2,1], dpi=300, facecolor = 'w', edgecolor = 'k', frameon=True)
ax = fig.add_axes([a,b,c,d])

states = [plt.Circle((sradius+x,sradius), radius=sradius, fc='y') for x in range(4)]

for state in states: ax.add_patch(state)
plt.xlim(0,5)
plt.ylim(0,10)
fig.show()
Mason Caiby
  • 1,846
  • 6
  • 17
0

There's a very similar example already: plot a circle with pyplot

However here's my take:

import matplotlib.pyplot as plt

sradius = .5

circle1 = plt.Circle((0.5, 0.5), sradius, color='r')
circle2 = plt.Circle((1.5, 0.5), sradius, color='blue')
circle3 = plt.Circle((2.5, 0.5), sradius, color='g', clip_on=False)

fig, ax = plt.subplots(figsize=(12,4))

ax.add_artist(circle1)
ax.add_artist(circle2)
ax.add_artist(circle3)

ax.set_xlim(0,3)

plt.show()

PS: People around here get pissy if you don't include import statements in your code.

J.Doe
  • 224
  • 1
  • 4
  • 19
  • you need to adjust the radius, figsize and set_xlim so that the circles don't get streched. – J.Doe Sep 26 '19 at 17:55