【问题标题】:How to specify the margin of a plot in mm/cm using matplotlib?如何使用 matplotlib 以 mm/cm 为单位指定绘图的边距?
【发布时间】:2011-12-21 20:30:24
【问题描述】:

示例

假设我有两个三角形:

  • 具有点 (0, 0)、(10, 0)、(10, 0.5) 和的三角形
  • 一个带点 (0, 0), (1, 0), (0.5, 11) 的三角形

在没有指定xlimylim的情况下生成的两个图如下所示:

问题

我需要做什么才能满足下面列出的所有点?

  • 使三角形可见,这样三角形的任何线都不会被轴隐藏
  • 以毫米、厘米或其他单位为所有绘图指定相同边距。
    (在上面的示例中,仅使用了两个三角形。实际上我有 n 三角形。)
    边距是指三角形外点与轴之间的距离。

生成的图应如下所示 不同的是,用红色箭头标记的距离应该全部相同!

【问题讨论】:

  • 我被你的问题弄糊涂了:你想调整情节外的边距吗?这如何影响轴覆盖线图的事实?如果您提供不同尺寸的纸张,您期望得到什么样的结果?您是否希望 A4 纸产生从 -.1 到 1.1 的 xlim,而信纸产生从 -.05 到 1.05 的 xlim?我认为边距与 xlim 和 ylim 值无关,也就是说,您可以有 2 英寸或 0.5 英寸的边距,并且在这两种情况下仍然有一个从 0 到 1 的 xlim。为什么你想要你想要的?
  • @Yann A4 纸与这个问题无关。我只是希望我的绘图可见,并在具有特定 dpi 分辨率的已定义纸张尺寸上以厘米为单位指定绘图到轴的边距。为什么我想要我想要的:然后我可以生成不同类型数据的相似外观图,并且与轴具有相同的边距。

标签: plot matplotlib axis margin


【解决方案1】:

您的两个点为 (0, 0), (10, 0), (10, 0.5) 和 (0, 0), (1, 0), (0.5, 11) 的三角形将在 pylab 中表示为:

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)

让我们看看最小的X值是多少:

lowestX = None

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

让读者对highestXlowestYhighestY做同样的练习。

现在,考虑 2 个单位的边界,您可以从最低和最高值中加/减这些单位并设置 xlimylim

margin = 2

pylab.xlim([lowestX-margin, highestX+margin])
pylab.ylim([lowestY-margin, highestY+margin])

【讨论】:

    【解决方案2】:

    我不知道以厘米/毫米为单位的方法,但你可以用总尺寸的百分比来做:

    # 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()
    

    输出是:

    修正后的图像:

    【讨论】:

    • 您好 Oz123,谢谢您的回答。但不幸的是,这并不能回答我的问题。关于您的绘图,我想以厘米或毫米为单位指定红点到轴的距离,以便您可以看到 (0,0)、(1,0) 和 (0,1) 的完整圆圈。
    • Woltan,我向您展示了一种以英寸为单位确定图形大小的方法。您可以计算如何将 cm 转换为英寸,并且使用 ylim 和 xlim 您可以设置轴的限制。看来您在这里编程并没有付出太多努力。如果您想手动绘制,我建议您放弃 matplotlib 并使用 Inkscape...
    • 我觉得有误会。我当然不想情节。设置x- 和ylim 与指定到轴的距离不同。图形大小也无济于事。也许我应该改写我的问题以澄清我所追求的......
    • 请用铅笔在纸上画出来,并用清晰的类型字母指定所有距离,然后扫描。我很想知道你在追求什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2015-10-09
    • 2023-03-30
    • 2021-07-17
    • 1970-01-01
    • 2013-01-20
    • 2018-11-09
    相关资源
    最近更新 更多