Matplotlib标题位置

2024-09-28 22:30:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个图,想更正标题名的位置。我想使垂直加速度(y)在垂直方向上位于左中,而闪光时间(x)在水平方向上位于中间机器人,也就是中间顶部的测试标题。基本上我希望能够移动标签的位置。 下面是代码

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter


x = ip.RESULTS
y = Vert

xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)

idx = z.argsort()
x, y, z = x[idx], y[idx], z[idx]

nullfmt = NullFormatter()         # no labels

# definitions for the axes
left, width = 0.1, 0.65
bottom, height = 0.1, 0.65
bottom_h = left_h = left + width + 0.02

rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom_h, width, 0.2]
rect_histy = [left_h, bottom, 0.2, height]

# start with a rectangular Figure
plt.figure(1, figsize=(8, 8))

axScatter = plt.axes(rect_scatter)
#plt.plot(np.unique(x), np.poly1d(np.polyfit(x, y, 1))(np.unique(x)))
#plt.plot(np.unique(x), np.poly1d(np.polyfit(x, y, 1))(np.unique(x)))
axHistx = plt.axes(rect_histx)
axHisty = plt.axes(rect_histy)

# no labels
axHistx.xaxis.set_major_formatter(nullfmt)
axHisty.yaxis.set_major_formatter(nullfmt)

# the scatter plot:
axScatter.scatter(x, y, c=z, s=50, edgecolor='')

# now determine nice limits by hand:
binwidth = 1
xymax = np.max([np.max(np.fabs(x)), np.max(np.fabs(y))])
lim = (int(xymax/binwidth) + 1) * binwidth

bins = np.arange(-lim, lim + binwidth, binwidth)
axHistx.hist(x)
axHisty.hist(y, orientation='horizontal')

plt.title('test title', fontsize=20)
axHisty.set_xlabel("Vertical Acceleration")
axHistx.set_xlabel("Flare Time")

结果是这样的。任何帮助都将不胜感激 enter image description here


Tags: rectimportnppltwidthleftuniqueset
1条回答
网友
1楼 · 发布于 2024-09-28 22:30:36

您的图形中有三个轴对象(打印矩形表示杂乱无章):axScatter是左下角的主图表axHisty是右侧的直方图,axHistx是顶部的直方图。您的轴标题位于axScatter的y轴和x轴上。所以,只要做:

axScatter.set_ylabel('Vertical Acceleration')
axScatter.set_xlabel('Flare Time')

基于你模糊的问题,我不知道你想把"test title"放在哪里,但只要找出哪个轴对象最好,然后给它一个xlabelylabeltitle

相关问题 更多 >