2

Generate figure with desired size and margins:

fig = plt.figure(figsize=(3,5))
ax = plt.Axes(fig,[left,bottom,width,height])
fig.add_axes(ax)

This along with legends, gridlines and everything else gives me what i'm expecting, except i can not remove the top and right axis. I referred to a similar question here, which directs me to a matplotlib example.

I tried

ax = Subplot(fig,111)
fig.add_subplot(ax)

ax.axis["top"].set_visible(False)
ax.axis["right"].set_visible(False)

stackoverflow would not let me post a picture yet because i don't have enough points so i hope my drawing will suffice.

_________
|        |
| |
| |
|_|
  |_________

The top and right axis of the front plot were removed (which is great!), but i have a second figure in the back that has nothing plotted.

I have tried looking at the matplotlib site, but i am still having a hard time understanding what exactly add_axes() and add_subplot() do.

Community
  • 1
  • 1
Casa
  • 2,247
  • 2
  • 15
  • 11

1 Answers1

2

Here is a solution which shows two possible ways to solve your problem:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.axislines import Subplot

left,bottom,width,height= -0.02 , 0.12, 1, 0.9
fig = plt.figure(figsize=(3,5))
ax1 = plt.Axes(fig,[left,bottom,width,height])
ax1.plot([1,2,3,4],'b') # plot on the first axes you created
fig.add_axes(ax1)

# using subplot you are acually using higher level objects

ax2 = Subplot(fig,111) # this addes another axis instance
fig.add_subplot(ax2)
ax2.axis["top"].set_visible(False)
ax2.axis["right"].set_visible(False)
ax2.plot([1,2,3,4,5],'r') # thos plots on the second

# now comment everything in ax2, and uncomment ax3
# you will get a crude, low level control of axes
# but both do what you want...

#ax3 = plt.Axes(fig,[left+0.2,bottom-0.2,width,height])
#ax3.plot([1,2,3,4],'g') # plot on the first axes you created

#for loc, spine in ax3.spines.iteritems():
#    if loc in ['left','bottom']:
#        spine.set_position(('outward',10)) # outward by 10 points
#    if loc in ['right','top']:
#        spine.set_color('none') # don't draw spine
#fig.add_axes(ax3)
plt.show()

The image is made with the crude low level ax3 object

oz123
  • 27,559
  • 27
  • 125
  • 187
  • The for loop in your post is exactly what i needed. I tried to show that it is usful, but i have to have 15 rep. Thanks Oz123 – Casa Nov 18 '11 at 16:14