【问题标题】:Latex \phantom not creating whitespace in matplotlibLatex \phantom没有在matplotlib中创建空格
【发布时间】:2015-07-13 05:16:17
【问题描述】:

朋友们,

我需要在图上放置 71 个刻度(它是蛋白质的序列),因此刻度需要交错排列,其中一些用星号突出显示:

________________________________
P o A i I A o g e u n e f e t r
 l t x s s L n S q e c O L t e s
*   *   * **   *       * *

我正在使用matplotlib.rc('text', usetex=True),以便我可以将星号向上移动(除非我减少它们的\vspace,否则它们在轴上显得太靠下)

问题在于 staggering 不起作用,任何将空格放在标签顶部的尝试都会被 LaTeX 吃掉。这是最小的情况(使每个标签都相同):

tickX = r'\noindent x\\\vspace{0.5cm}\\y\\ \vspace{-0.5cm}\\z'

这很好用,我得到了我想要的间距。但是,我只希望xy 出现在剧情中,所以我将\phantom 去掉了我不想要的:

tickX = r'\noindent \phantom{x}\\\vspace{0.5cm}\\y\\ \vspace{-0.5cm}\\z'

(在此示例中,ax.set_xticklabels 提供了tickX 的列表。在实际代码中,每个刻度都是不同的。)

但是现在换行符消失了,ys 出现在 xs 应该在的位置!我找到了\vspace*{},但我也没有运气。我也试过\null\mbox{}

如果我放弃 LaTeX 并只使用 ' \ny\nz',我会得到所需的行为,除了 z 显得太低,我又回到了我开始的地方。

所以,我的问题是:如何在 LaTeX 处理的刻度标签顶部插入一个空行?或者,等效地,如何在 LaTeX 中创建一个可以在文档开头单独占据一行的幻像字符?

【问题讨论】:

    标签: python matplotlib latex spacing


    【解决方案1】:

    好吧,这很奇怪。没有多少搜索导致matplotlib实际上在开头插入了一个空格。所以,我在每个标签的顶部放了一个句点,因此:

    ________________________________
    ................................
    P o A i I A o g e u n e f e t r
     l t x s s L n S q e c O L t e s
    *   *   * **   *       * *
    

    并在每个句点后插入\vspace

    def xTickString(index, sequence, decorations):
        ret = r"\noindent "
        if(index % 2 == 0): #Put the tick at the top...
            ret += r'.\\\vspace{29.1em}\\\makebox[0pt]{'
            ret += sequence[index]
            ret += r'}\\\vspace{0.2em}\\'
            ret += '\makebox[0pt]{\**}' if decorations[index] == '*' else ''
        else:
            ret += r'.\\\vspace{30em}\\\makebox[0pt]{'
            ret += sequence[index]
            ret += r'}\\ \vspace{-0.7em}\\'
            ret += '\makebox[0pt]{\**}' if decorations[index] == '*' else ''
        return ret 
    

    当然,如果使用 `\vspace{30em},句点会出现在刻度旁边,并且实际的标签会超出页面。因此,现在您在配置轴时相对于刻度偏移标签:

    ax.tick_params(axis='x', pad=-494)
    

    它从情节的顶部运行句点并将标签放在正确的位置。

    零宽度框使字母与标签中的星号保持一致,否则间距会变得不稳定。

    【讨论】:

      猜你喜欢
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 2017-12-12
      • 2018-05-26
      • 1970-01-01
      相关资源
      最近更新 更多