使用matplotlib在X轴下方添加标题以绘制散点图 [英] Adding caption below X-axis for a scatter plot using matplotlib

查看:49
本文介绍了使用matplotlib在X轴下方添加标题以绘制散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am pretty new to python and to the matplotlib library. I have created a scatter plot using matplotlib and now I wish to add caption a little below the X-axis. This is my code:

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

As you can see in the image my caption is way below the scatter plot, is there a way to bring it exactly below the X-axis? Also my scatter plot looks rectangular, is there a way to make it square like?

推荐答案

类似的东西

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上游,使此操作更容易进行,但我们仍在寻找可以做到的人.

might work. Making this easier to do is on the radar for mpl upstream, but we are still looking for someone to do it.

这篇关于使用matplotlib在X轴下方添加标题以绘制散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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