Reputation: 5999
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
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)
Upvotes: 2
Views: 4066
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:
Upvotes: 3
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:
Actually you may set tick labels as you want.
Upvotes: 3
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()
Upvotes: 2