【问题标题】:How to set axes dimensions and locations in millimeters in matplotlib?如何在 matplotlib 中以毫米为单位设置轴尺寸和位置?
【发布时间】:2016-01-19 06:22:27
【问题描述】:

我需要一个轴宽 80 毫米、高 60 毫米的图形,而标签尺寸为 12 磅。图形大小必须严格裁剪,边界框外没有空间。我该怎么做呢?

另外,我想要两个间距为 10 毫米的轴堆叠。我该怎么做?


点是每英寸 72 点或每毫米 72/25.4 点的标准度量。 边界框是包含图形所有“墨水”的最小框。

标签大小很容易,因为它们以磅为单位定义。修复依赖于内容大小的图形大小很难。

提出这个问题的原因是需要创建多个看起来完全相同的出版质量图。此外,通过乳胶渲染,字符的大小和形状可以与正文完全相同。这也适用于PowerPoint演示文稿。一切都归结为每个字体大小的图形大小,必须固定为标准单位。

https://en.wikipedia.org/wiki/Point_(typography)

1 分(排版)=

SI 单位 352.78×10−6 m 352.778 μm

美国习惯单位(英制单位)

1.1574×10−3 英尺 13.889×10−3 英寸

【问题讨论】:

    标签: python matplotlib geometry point figure


    【解决方案1】:

    你(我)不想那样。最好的方法是以毫米为单位定义图形宽度以毫米为单位的轴宽,并将轴+标签居中。高度无关紧要,应该为空白修剪。

    这只是一个开始,缺少居中。下一步是清理代码并使其成为函数(并在菜单中添加“修剪高度”和“修剪宽度”按钮)。

    基本上,这将是一个两步过程。第一步,定义轴的大小并用标签绘制图形。在第 2 步中,调用了tightbox 函数并以英寸为单位计算图形的宽度。然后重新调整图形并确定新的轴位置。

    import matplotlib
    import pylab
    
    matplotlib.rc('text', usetex=True)
    matplotlib.rc('figure', dpi=72)
    font = {'family' : 'normal',
            'size'   : 10}
    matplotlib.rc('font', **font)
    matplotlib.rcParams['text.latex.preamble'] = [
           r'\usepackage{lmodern}'    # latin modern, recommended to replace computer modern sans serif
           r'\usepackage{siunitx}',   # i need upright \micro symbols, but you need...
           r'\sisetup{detect-all}',   # ...this to force siunitx to actually use your fonts
           r'\usepackage{helvet}',    # set the normal font here
           r'\usepackage{sansmath}',  # load up the sansmath so that math -> helvet
           r'\sansmath']  # <- tricky! -- gotta actually tell tex to use! 
    
    matplotlib.rcParams['xtick.major.pad'] = 3 # ticklabel spacing between axis and text
    matplotlib.rcParams['ytick.major.pad'] = 2 # 
    
    # APS, PRL, Two Columns
    column_width_mm     = 86.4581 #mm
    column_height_mm    = 2.5*column_width_mm
    
    fig_hspace_mm       = 10 # mm
    fig_wspace_mm       = 10 # mm
    
    axis_width_mm       = 69.16648
    axis_height_mm      = 43.09763
    
    column_width_inch   = column_width_mm/25.4
    column_height_inch  = column_height_mm/25.4
    
    trim_height_below_mm = 0.0
    trim_height_above_mm = 0.0
    
    pylab.figure(num = 1, figsize=(column_width_inch, column_height_inch))
    
    ax_0   = pylab.axes([fig_wspace_mm/column_width_mm, (1.0*fig_hspace_mm + 0.0*axis_height_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    ax_1   = pylab.axes([fig_wspace_mm/column_width_mm, (2.0*fig_hspace_mm + 1.0*axis_height_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    ax_2   = pylab.axes([fig_wspace_mm/column_width_mm, (3.0*fig_hspace_mm + 2.0*axis_height_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    ax_3   = pylab.axes([fig_wspace_mm/column_width_mm, (4.0*fig_hspace_mm + 3.0*axis_height_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    
    ax_0.set_position([fig_wspace_mm/column_width_mm, (1.0*fig_hspace_mm + 0.0*axis_height_mm - trim_height_below_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    ax_1.set_position([fig_wspace_mm/column_width_mm, (2.0*fig_hspace_mm + 1.0*axis_height_mm - trim_height_below_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    ax_2.set_position([fig_wspace_mm/column_width_mm, (3.0*fig_hspace_mm + 2.0*axis_height_mm - trim_height_below_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    ax_3.set_position([fig_wspace_mm/column_width_mm, (4.0*fig_hspace_mm + 3.0*axis_height_mm - trim_height_below_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    
    ###########################
    # plot figure here
    ###########################
    pylab.sca(ax_0)
    pylab.xlabel(r'$\mathrm{10pt~Test~Label~with~huge~symbols:}~\int~~\mathrm{[\frac{m}{s}]}$')
    ###########################
    
    fig = pylab.gcf()
    
    old_size        = fig.get_size_inches()
    old_width_mm    = old_size[0]*25.4
    old_height_mm   = old_size[1]*25.4 
    
    bbox3 = ax_3.get_tightbbox(pylab.gcf().canvas.get_renderer())
    bbox0 = ax_0.get_tightbbox(pylab.gcf().canvas.get_renderer())
    
    trim_height_below_mm = ((bbox0.ymin)/72.0*25.4)
    trim_height_above_mm = old_height_mm - ((bbox3.ymax+4)/72.0*25.4)
    
    new_size        = fig.get_size_inches()
    new_width_mm    = new_size[0]*25.4 
    new_height_mm   = new_size[1]*25.4 - trim_height_below_mm - trim_height_above_mm
    
    fig.set_size_inches(new_width_mm/25.4, new_height_mm/25.4, num = 1, forward=True)
    
    print new_size, old_size, new_width_mm, new_height_mm
    print trim_height_below_mm, trim_height_above_mm
    
    column_width_mm     = new_width_mm
    column_height_mm    = new_height_mm 
    
    ax_0.set_position([fig_wspace_mm/column_width_mm, (1.0*fig_hspace_mm + 0.0*axis_height_mm - trim_height_below_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    ax_1.set_position([fig_wspace_mm/column_width_mm, (2.0*fig_hspace_mm + 1.0*axis_height_mm - trim_height_below_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    ax_2.set_position([fig_wspace_mm/column_width_mm, (3.0*fig_hspace_mm + 2.0*axis_height_mm - trim_height_below_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    ax_3.set_position([fig_wspace_mm/column_width_mm, (4.0*fig_hspace_mm + 3.0*axis_height_mm - trim_height_below_mm)/column_height_mm, axis_width_mm/column_width_mm, axis_height_mm/column_height_mm])
    
    pylab.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      相关资源
      最近更新 更多