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.

4
  • Did you close the figure between the two blocks of commands? If you leave it open, matplotlib defaults to drawing onto your current figure without clearing it. Commented Nov 18, 2011 at 1:00
  • I don't think so. What do you have to do to assure that the figure is closed? Commented Nov 18, 2011 at 16:16
  • I usually just close the plot window. Commented Nov 18, 2011 at 17:06
  • Oh haha, yes i close it. Thanks Commented Nov 18, 2011 at 18:42

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.