【问题标题】:Adding caption below X-axis for a scatter plot using matplotlib使用 matplotlib 在 X 轴下方为散点图添加标题
【发布时间】:2015-11-30 23:52:38
【问题描述】:

我对 python 和 matplotlib 库还很陌生。我已经使用 matplotlib 创建了一个散点图,现在我希望在 X 轴下方添加一点标题。这是我的代码:

from matplotlib import pyplot as plt
import numpy as np
from pylab import *

file = open('distribution.txt', 'r')

txt="I need the caption to be present a little below X-axis"

x=[]
y=[]
for line in file:
    new=line.rstrip()
    mystring=new.split("\t")
    x.append(mystring[0])
    y.append(mystring[1])


fig = plt.figure()
ax1 = fig.add_axes((0.1,0.4,0.8,0.5))
ax1.set_title("This is my title")
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Y-axis')
ax1.scatter(x,y, c='r')
fig.text(.05,.05,txt)
plt.xlim(0, 1.05)
plt.ylim(0, 2.5)
plt.show()

正如您在图像中看到的那样,我的标题在散点图下方,有没有办法将它精确地放在 X 轴下方?另外我的散点图看起来是矩形的,有没有办法让它像方形?

【问题讨论】:

    标签: matplotlib plot


    【解决方案1】:

    您可以简单地使用figtext。您还可以根据需要更改 x 和 y 轴的值。

    txt="I need the caption to be present a little below X-axis"
    plt.figtext(0.5, 0.01, txt, wrap=True, horizontalalignment='center', fontsize=12)
    

    【讨论】:

    • 它可以工作,但不如fig.text 面向对象。 +1
    【解决方案2】:

    类似:

    from matplotlib import pyplot as plt
    import numpy as np
    
    txt="I need the caption to be present a little below X-axis"
    
    # make some synthetic data
    x = np.linspace(0, 1, 512)
    y = np.random.rand(512)*2.3 + .1
    
    fig = plt.figure()
    ax1 = fig.add_axes((0.1, 0.2, 0.8, 0.7))
    
    ax1.set_title("This is my title")
    ax1.set_xlabel('X-axis')
    ax1.set_ylabel('Y-axis')
    
    # make the edge colors match the facecolors
    ax1.scatter(x,y, c='r', edgecolors='face')
    # center text
    fig.text(.5, .05, txt, ha='center')
    
    # use OO interface    
    ax1.set_xlim([0, 1.05])
    ax1.set_ylim([0, 2.5])
    
    # resize the figure to match the aspect ratio of the Axes    
    fig.set_size_inches(7, 8, forward=True)
    
    plt.show()
    

    可能会奏效。使这更容易做到这一点是 mpl 上游的雷达,但我们仍在寻找人来做这件事。

    【讨论】:

    • 5年后,似乎没有更好的解决方案。 +1。请注意,y pos 值可以为负数,以增加 x 标签和图例之间的空间。
    • 我们刚刚合并(如 2 天前)一个 PR 以将与此类似的功能放入主线 Matplotlib。应该在 mpl3.4 (Jan21) github.com/matplotlib/matplotlib/pull/17524 中可用
    【解决方案3】:

    首先,我对发布针对 matplotlib 的联合首席开发人员的答案感到很奇怪。显然,@tacaswell 比我更了解 matplotlib。但与此同时,他的回答对我来说不够有活力。我需要一个始终基于xlabel 位置的标题,并且不能只使用文本注释。

    我考虑过简单地更改 xlabel 以添加换行符和标题文本,但这并不能清楚地区分标题,并且您不能执行更改文本大小或使其中间斜体等操作一个文本字符串。

    我通过使用 matplotlib 的 TeX 功能解决了这个问题。这是我的解决方案:

    from matplotlib import pyplot as plt
    from matplotlib import rc
    import numpy as np
    from pylab import *
    
    rc('text', usetex=True)
    
    file = open('distribution.txt', 'r')
    
    txt="I need the caption to be present a little below X-axis"
    
    x=[]
    y=[]
    for line in file:
        new=line.rstrip()
        mystring=new.split("\t")
        x.append(mystring[0])
        y.append(mystring[1])
    
    
    fig = plt.figure()
    ax1 = fig.add_axes((0.1,0.4,0.8,0.5))
    ax1.set_title("This is my title")
    ax1.set_xlabel(r'\begin{center}X-axis\\*\textit{\small{' + txt + r'}}\end{center}')
    ax1.set_ylabel('Y-axis')
    ax1.scatter(x,y, c='r')
    plt.xlim(0, 1.05)
    plt.ylim(0, 2.5)
    plt.show()
    

    我对 tacaswell 的回答中的随机散点图做了同样的事情,这是我的结果:

    一个警告:如果您调整它以获取输入字符串变量,则字符串可能无法正确转义以用于 TeX。转义 LaTeX 代码已经在 Stack Overflow 上进行了介绍,地址为 https://stackoverflow.com/a/25875504/1404311。我直接使用它,然后可以任意 xlabels 和标题。

    【讨论】:

      【解决方案4】:

      正如@Nicky V 提到的那样。

      plt.xlabel('''Butterfly contract traded
      
      Note: c1-c2-c3 indicates the position: long 2 times c2 and short c1 anc c3''')
      

      结果:

      【讨论】:

        【解决方案5】:

        当我无法弄清楚如何以设计的方式进行操作时,我使用的另一个简单解决方案......如果这有意义的话,就是在 xlabel plt.xlabel("xaxis label\n\n\n\ncaption") 这只是在 x 轴下方添加了几行新的标题,这使它看起来像一个标题,即使它实际上是 xaxis 标签的一部分

        【讨论】:

        • 是的,在使用figtext之后,我也知道\nxlabel中有效。感谢您在此处发布。
        猜你喜欢
        • 2017-08-24
        • 1970-01-01
        • 1970-01-01
        • 2011-12-16
        • 2021-11-16
        • 2016-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多