Latex \ phantom无法在matplotlib中创建空格 [英] Latex \phantom not creating whitespace in matplotlib

查看:116
本文介绍了Latex \ phantom无法在matplotlib中创建空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友

我需要绘制71个刻度(这是蛋白质的序列),所以刻度需要错开,有些用星号突出显示:

I have 71 ticks that I need to put on a plot (it's the sequence of a protein) and so the ticks need to be staggered, and some are highlighted with asterisks:

________________________________
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 )

I'm using matplotlib.rc('text', usetex=True) so that I can move the asterisks up (they appear too far down on the axis unless I decrease their \vspace)

问题在于交错不起作用,任何将空白放在标签顶部的尝试都会被LaTeX吞噬.这是最小的情况(使每个标签都相同):

The problem is that the staggering doesn't work, any attempts to put whitespace at the top of the label get eaten by LaTeX. Here's the minimal case (makes each label the same):

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

这很好,我得到了想要的间距.但是,我只希望 x y 出现在情节上,所以我 \ phantom 删除了我不想要的一个:

This works fine, I get the spacing I want. However, I only want x or y to appear on the plot, so I \phantom out the one I don't want:

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

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

(A list of tickX is provided to ax.set_xticklabels, in this example. In the real code, each tick is different.)

但是现在换行符消失了,并且 y s出现在 x s应该出现的位置!我找到了 \ vspace * {} ,但是我也没有运气.我还尝试了 \ null \ mbox {} .

But now the newline disappears and the ys appear just where the xs should be! I've found \vspace*{}, but I've had no luck with that either. I've also tried \null and \mbox{}.

如果我放弃LaTeX并仅使用'\ ny \ nz',则除了 z 显得太低之外,我将获得所需的行为,并且我回到了起点

If I forgo LaTeX and just use ' \ny\nz', I get the desired behavior except the z appears too low and I'm back where I started.

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

So, my question is: How do I insert a blank line at the top of a LaTeX-processed tick label? Or, equivalently, how do I create a phantom character in LaTeX that can occupy a line by itself at the beginning of the document?

推荐答案

好的,这很奇怪.没有大量搜索导致matplotlib实际上在开头插入了空格.因此,我在每个标签的顶部放置了一个句点,从而:

Okay, this was a weird one. No amount of searching resulted in matplotlib actually inserting a blank space at the beginning. So, I put a period at the top of each label, thus:

________________________________
................................
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 :

and inserted a \vspace after each period:

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},句号显示在对勾旁边,并且实际标签不在页面上.因此,现在在配置轴时相对于刻度线偏移标签:

Of course with a `\vspace{30em}, the period appears next to the tick and the actual label runs off the page. So, now you offset the label relative to the tick when you configure the axis:

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

在句点顶部运行句点,并将标签放置在正确的位置.

which runs the periods off the top of the plot and puts the labels in just the right place.

零宽度的框使字母与标签中的星号对齐,否则间距会不稳定.

The zero-width boxes keep the letters in line with their asterisks in the labels, otherwise the spacing gets erratic.

这篇关于Latex \ phantom无法在matplotlib中创建空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆