4

Example

Suppose I have two triangles:

  • A triangle with points (0, 0), (10, 0), (10, 0.5) and
  • a triangle with points (0, 0), (1, 0), (0.5, 11)

The resulting two plots without specifying the xlim and ylimlook like this: enter image description here

Question

What do I need to do to satisfy all points listed below?

  • Make the triangle visible, so that no line of the triangle is hidden by an axis
  • Specify the same margin for all plots in mm, cm or other unit.
    (in the example above only two triangles were used. Actually I have n triangles.)
    As margin I mean the distance between the outer points of the triangle and the axis.

The resulting plots should look like this enter image description here with the difference that the distances, which are marked with the red arrows, should all be the same!

Community
  • 1
  • 1
Woltan
  • 13,723
  • 15
  • 78
  • 104
  • I'm confused by your question: do you want to adjust the margin outside the plot? How does this affect the fact that the axis covers your line plot? What kind of result are you expecting if you gave a different size of paper? Do you want A4 paper to produce xlims from -.1 to 1.1 and letter paper to produce xlims from -.05 to 1.05? I thought the margin is unrelated to the xlim and ylim values, that is you can have 2 inch or .5 inch margins and still have an xlim from 0 to 1 in both cases. Why do you want what you want? – Yann Nov 04 '11 at 12:38
  • @Yann The A4 paper is irrelevant in this question. I simply want my plot to be visible and specify the margin from the plot to the axis in cm on a defined paper size with a specific *dpi* resolution. Why I want what I want: Then I could generate similar looking plots of different kind of data with the same margins to the axis. – Woltan Nov 07 '11 at 09:30

2 Answers2

0

Your two triangles with points (0, 0), (10, 0), (10, 0.5) and (0, 0), (1, 0), (0.5, 11) would be represented in pylab as:

Ax = [0, 10, 10]
Ay = [0, 0, 0.5]
Bx = [0, 1, 0.5]
By = [0, 0, 11]

pylab.plot(Ax, Ay)
pylab.plot(Bx, By)

Let's see what the lowest X value is:

lowestX = None

for x in Ax+Bx:
    if lowestX==None or x<lowestX:
        lowestX = x

Exercise for the reader to do the same for highestX, lowestY, and highestY.

Now, consider a boundary of 2 units, you can add / subtract these units from the lowest and highest values and set xlim and ylim:

margin = 2

pylab.xlim([lowestX-margin, highestX+margin])
pylab.ylim([lowestY-margin, highestY+margin])
dotancohen
  • 30,064
  • 36
  • 138
  • 197
0

I don't know of a way to to it in cm/mm, but you can do it with the precentage of the total size:

# you don't really need this see bellow
#from matplotlib.backends.backend_pdf import PdfPages
import pylab
import matplotlib.pyplot as plt

left,bottom,width,height = 0.2,0.1,0.6,0.6 # margins as % of canvas size 

fig = plt.figure(figsize=(4,4),facecolor="yellow") # figure size in Inches
fig.patch.set_alpha(0.8) # just a trick so you can see the difference 
                         # between the "canvas" and the axes
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)

ax1.plot([0,1,1,0,0], [0,0,1,1,0],"ro") # plot on the first axes you created
ax1.set_xlim([-1.1,2]) 
ax1.set_ylim([-1.1,2]) 

# pylab.plot([0,1,1,0,0], [0,0,1,1,0],"ro") avoid usig if you 
# want to control more frames in a plot
# see my answer here
#http://stackoverflow.com/questions/8176458/\
#remove-top-and-right-axis-in-matplotlib-after-\
#increasing-margins/8180844#8180844

# pdf = PdfPages("Test.pdf")# you don't really need this
# pylab.savefig(pdf, papertype = "a4", format = "pdf")
# automagically make your pdf like this
pylab.savefig("Test1.pdf", papertype="a4",facecolor='y')  
pylab.show()
pylab.close()       
# pdf.close()

and the output is:

image with margins

corrected image

corrected image:

oz123
  • 27,559
  • 27
  • 125
  • 187
  • Hi Oz123, thank you for your answer. But unfortunately this does not answer my question. Regarding your plot, I would want to specify the distance of the red dots to the axis in cm or mm, so that you can see the full circle of (0,0), (1,0) and (0,1). – Woltan Nov 23 '11 at 08:39
  • Woltan, I showed you a way to determine the figure size in inches. You can calculate how to convert cm to inches, and using ylim and xlim you can set the limits of the axis. it seems you are not making to much effort to program here. If you want to draw it manually, i'd recommend you give up matplotlib and go Inkscape... – oz123 Nov 23 '11 at 09:02
  • I think there is a misunderstanding. Of course I do not want to *draw* a plot. Setting the `x`- and `ylim` is not the same as specifying the distance to the axis. Also the figure size does not help. Maybe I should rephrase my question to clarify what I am after... – Woltan Nov 23 '11 at 09:18
  • Please draw this with a Pencil on a paper, and specify all distances with clear Type letters, and scan it. I am curious to see what you are after. – oz123 Nov 23 '11 at 09:22