使用matplotlib在散点图的X轴下方添加标题

使用matplotlib在散点图的X轴下方添加标题,matplotlib,plot,Matplotlib,Plot,我对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

我对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轴下方?另外,我的散点图看起来是长方形的,有没有办法让它变成正方形

类似于:

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 upstream的雷达上已经出现了使这一点更容易实现的情况,但我们仍在寻找能够做到这一点的人

首先,我觉得发布一个反对matplotlib联合开发负责人的答案很奇怪。显然,@tacaswell比我更了解matplotlib。但同时,他的回答对我来说不够有力。我需要一个总是基于
xlabel
位置的标题,不能只使用文本注释

我考虑简单地改变<代码> XLabe<代码>来添加换行符和字幕文本,但是这不能清楚地区分字幕,而且不能做改变文本大小或使文本字符串中间斜体的事情。

我通过使用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()
我对塔卡斯韦尔答案中的随机散点图做了同样的处理,下面是我的结果:


一个警告:如果您调整它以获取输入字符串变量,字符串可能无法正确转义以用于TeX。转义LaTeX代码已在堆栈溢出中覆盖,位于。我直接使用它,然后可以使用任意的xlabel和标题。

您只需使用
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)

另一个简单的解决方案是,当我不知道如何用设计好的方式来实现时,我使用的方法是,只要在xlabel plt.xlabel(“xaxis标签\n\n\n\n选项”)中添加一些新行即可这只是在x轴下添加了几行新的标题,这使得它看起来像一个标题,尽管它实际上是xaxis标签的一部分,正如@Nicky V所提到的

plt.xlabel('''Butterfly contract traded

Note: c1-c2-c3 indicates the position: long 2 times c2 and short c1 anc c3''')
结果:


是的,在使用了
figtext
之后,我也知道
\n
xlabel
中是有效的。感谢您在这里发布。5年后,似乎没有更好的解决方案+1.注:y位置值可以为负值,以增加x标签和地物图例之间的空间。我们刚刚合并了一个PR(如2天前所述),以便在主线Matplotlib中添加类似的功能。应该在mpl3.4(Jan21)中提供,它可以工作,但不像mpl3.4那样面向对象+1.