Flux Capacitor
Flux Capacitor

Reputation: 1231

Trouble setting plot axis limits with matplotlib / python

I have some code:

def print_fractures(fractures):
    # generate ends for the line segments
    xpairs = []
    ypairs = []
    plt.subplot(132)

    for i in range(len(fractures)):
        xends = [fractures[i][1][0], fractures[i][2][0]]
        yends = [fractures[i][1][1], fractures[i][2][1]]
        xpairs.append(xends)
        ypairs.append(yends)
    for xends,yends in zip(xpairs,ypairs):
        plt.plot(xends, yends, 'b-', alpha=0.4)
        plt.plot()
    plt.xlabel("X Coordinates (m)")
    plt.ylabel("Y Coordinates (m)")
    plt.ylim((0,400))


def histogram(spacings):
    plt.subplot(131)
    plt.hist(np.vstack(spacings), bins = range(0,60,3), normed=True)
    plt.xlabel('Spacing (m)')
    plt.ylabel('Frequency (count)')
    #plt.title("Fracture Spacing Histogram")


def plotCI(sample, ciHigh, ciLow, avgInts):
    plt.subplot(133)    
    plt.plot(sample,ciHigh)
    plt.plot(sample,avgInts)
    plt.plot(sample,ciLow)
    plt.legend(['Mean + 95% CI', 'Mean', 'Mean - 95% CI'])
    plt.title("Intersections vs Number of Wells")
    plt.xlabel("# of Wells")
    plt.ylabel("# of Intersections for " + str(bedT) + "m bed thickness")

def makeplots(spacings,fractures): 
    histogram(spacings)
    print_fractures(fractures)
    plt.axis('equal')
    plotCI(sample, ciHigh, ciLow, avgInts)
    plt.show()
makeplots(spacings,fractures)

The code produces the following plot: enter image description here

As you can see, in the center plot, the plot is not really centered... I would like to set the x and y axes to (0,400) but I am having troubles.

So far I have tried:

plt.axis((0,400,0,400))

and:

plt.ylim((0,400))
plt.xlim((0,400))

Neither option worked for me.

Upvotes: 1

Views: 1866

Answers (1)

Dietrich
Dietrich

Reputation: 5551

In your example, it is not shown how / when the functions are called. There are some possible side effects concerning plt.ion()/plt.ioff() and the focus of your actual plot at the time of executing xlim()/ylim() commands.

To have full control (especially, when having more than one plot), it is usually better to have explicit figure and plot handles, e.g:

fg = plt.figure(1)
fg.clf()  # clear figure, just in case the script is not executed for the first time 
ax1 = fg.add_subplot(2,1,1)
ax1.plot([1,2,3,4], [10,2,5,0])
ax1.set_ylim((0,5)) # limit yrange

ax2 = fg.add_subplot(2,1,2)    
...

fg.canvas.draw()  # the figure is drawn at this point

plt.show()  # enter GUI event loop, needed in non-interactive interpreters

Upvotes: 2

Related Questions