Search code examples
pythonmatplotlibfigureaxes

How to set figure axis range


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?


Solution

  • 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.