python - 可变大小图形的图例位置
问题描述
我的绘图功能每年为不同大小的数据创建水平条。我必须更改每组子图的图形大小。我需要将我的两个图例放在 x 轴标签下方每个图形的下中心。位置需要根据图形大小而变化并保持一致。所以对于所有生产的人物,传说看起来像这个人物。
在这里找到我的数据框片段。我试图尽可能地简化代码,我知道情节缺少一些元素,但我只想得到我的问题的答案,而不是在这里创建一个完美的情节。我知道我可能需要为我的锚定边界框创建一个变量,但我不知道如何。这是我的代码:
def plot_bars(data,ax):
""" Plots a single chart of work plan for a specific routeid
data: dataframe with section length and year
Returns: None"""
ax.barh(df['year'], df['sec_len'] , left = df['sec_begin'])
ax.set_yticklabels('')
def plot_fig(df):
# Draw the plots
ax_set = df[['routeid','num_bars']].drop_duplicates('routeid')
route_set = ax_set['routeid'].values
h_ratios = ax_set['num_bars'].values
len_ratio = h_ratios.sum()/BARS_PER_PAGE # Global constant set to 40 based on experiencing
fig, axes = plt.subplots(len(route_set), 1, squeeze=False, sharex=True
, gridspec_kw={'height_ratios':h_ratios}
, figsize=(10.25,7.5*len_ratio))
for i, r in enumerate(route_set):
plot_bars(df[df['routeid']==r], axes[i,0])
plt.xlabel('Section length')
## legends
fig.legend(labels=['Legend2'], loc=8, bbox_to_anchor=(0.5, -0.45))
fig.legend( labels=['Legend1'], loc = 8, bbox_to_anchor=(0.5, -0.3))
## Title
fig.suptitle('title', fontsize=16, y=1)
fig.subplots_adjust(hspace=0, top = 1-0.03/len_ratio)
for df in df_list:
plot_fig(df)
问题是当图形大小发生变化时,图例会像这些图片一样移动:
解决方案
我认为问题归结为相对于 具有正确的相对位置xlabel
,因此您需要bbox_to_anchor
使用 xlabel 的位置和轴的高度/宽度来计算是正确的。像这样的东西:
fig, (ax, ax1) = plt.subplots(nrows=2, figsize=(5, 4), gridspec_kw={'height_ratios':[4, 1]})
ax.plot(range(10), range(10), label="myLabel")
ax.set_xlabel("xlabel")
x, y = ax.xaxis.get_label().get_position() # position of xlabel
h, w = ax.bbox.height, ax.bbox.width # height and width of the Axes
leg_pos = [x + 0 / w, y - 55 / h] # this needs to be adjusted according to your needs
fig.legend(loc="lower center", bbox_to_anchor=leg_pos, bbox_transform=ax.transAxes)
plt.show()
推荐阅读
- javascript - putImageData 脏宽度和高度不起作用
- c# - ImportError:导入 _fblas 时 DLL 加载失败,当通过 CFFI 从 Visual Studio C# 运行 Python 时
- javascript - 可以添加
像班级这样的价值观? - python - gzip 压缩时设备上没有空间
- sapui5 - UI5 i18n 支持静态模型数据
- instagram-graph-api - 访问授权帐户的所有 Instagram Story 帖子和见解
- typescript - 如何将 UserData 添加到 AWS CDK 中的 AWS LaunchTemplate
- prometheus - 在普罗米修斯中将计数器值初始化为0
- r - 如何减去R中所有列中的不同值
- oracle - 更改 Oracle ADW 默认时区