Chan Kim
Chan Kim

Reputation: 5999

how to make x,y axis appear in an axes in python matplotlib

I have a text file (which I extracted during run using np.savetxt function) _anchors.out

-84 -40 99 55
-176 -88 191 103
-360 -184 375 199
-56 -56 71 71
-120 -120 135 135
-248 -248 263 263
-36 -80 51 95
-80 -168 95 183
-168 -344 183 359

These are 9 boxes centered around point 7.5. I can see the boxes using code below(qq.py)

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches

b = np.loadtxt('_anchors.out','int')
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.set_xbound(-400,400)
ax1.set_ybound(-400,400)
for x1,y1,x2,y2 in b:
    ax1.add_patch(patches.Rectangle((x1,y1), x2-x1, y2-y1,fill=False))
fig1.show()

and the figure is graph with no axis
But I want to see the X, and Y axis together(not the 'axes' in matplotlib term). I've searched some solution but it's not clear to me. How should I modify above code? (you can check it using ipython. run ipython, and inside type 'run qq.py')

What I want to see is below(X and Y axes added)here

Upvotes: 2

Views: 4066

Answers (3)

Ianhi
Ianhi

Reputation: 3162

Following the documentation here you can achieve this with the following code:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches

b = np.loadtxt('_anchors.out','int')
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.set_xbound(-400,400)
ax1.set_ybound(-400,400)
for x1,y1,x2,y2 in b:
    ax1.add_patch(patches.Rectangle((x1,y1), x2-x1, y2-y1,fill=False))

#Change spine position
ax1.spines['left'].set_position('center')
ax1.spines['right'].set_color('none')
ax1.spines['bottom'].set_position('center')
ax1.spines['top'].set_color('none')
ax1.spines['left'].set_smart_bounds(True)
ax1.spines['bottom'].set_smart_bounds(True)
ax1.xaxis.set_ticks_position('bottom')
ax1.yaxis.set_ticks_position('left')

#Show figure
fig1.show()

Giving:

enter image description here

Upvotes: 3

Serenity
Serenity

Reputation: 36695

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches

b = np.loadtxt('./_anchors.out','int')
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.set_xbound(-400,400)
ax1.set_ybound(-400,400)
for x1,y1,x2,y2 in b:
    ax1.add_patch(patches.Rectangle((x1,y1), x2-x1, y2-y1,fill=False))

# Move left y-axis and bottom x-axis to center of the figure
ax1.spines['left'].set_position('center')
ax1.spines['bottom'].set_position('center')
ax1.spines['left'].set_color('r')
ax1.spines['bottom'].set_color('r')
# Show ticks in the left and lower axes only
ax1.xaxis.set_tick_params(bottom='on', top='off', direction='inout', color='r')
ax1.yaxis.set_tick_params(left='on', right='off', direction='inout', color='r')    

# Remove upper and right axes
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)

plt.show()

Output:

enter image description here

Actually you may set tick labels as you want.

Upvotes: 3

Pedro
Pedro

Reputation: 293

You can use this alternative that plots dashed lines:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches

b = np.loadtxt('_anchors.out','int')
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.set_xbound(-400,400)
ax1.set_ybound(-400,400)
for x1,y1,x2,y2 in b:
    ax1.add_patch(patches.Rectangle((x1,y1), x2-x1, y2-y1,fill=False))
plt.plot((0, 0), (-400, 400), 'r--')
plt.plot((-400, 400), (0, 0), 'r--')
fig1.show()

Which outputs: enter image description here

Upvotes: 2

Related Questions