如何在matplotlib中将轴标签对齐到右侧或顶部? [英] How to align axis label to the right or top in matplotlib?

查看:126
本文介绍了如何在matplotlib中将轴标签对齐到右侧或顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下, matplotlib 将轴标签绘制在轴的中心.我想以水平轴和垂直轴都与轴的末端对齐的方式移动标签.例如,对于水平轴,我希望看到:

  + -------------------- +||||||||||+ -------------------- +标签 

是否可以使用matplotlib的全局设置来做到这一点?

解决方案

By default matplotlib plots the axis label at the center of the axis. I would like to move the label in such way that it is aligned with the end of the axis, both for the horizontal and vertical axis. For example for the horizontal axis I would like to see:

+--------------------+
|                    |
|                    |
|                    |
|                    |
|                    |
+--------------------+
                 label

Is it possibile to do it with the global setting of matplotlib?

解决方案

As of v3.3 you can now set these with the loc argument of set_xlabel and set_ylabel:

import matplotlib.pyplot as plt

# Left plot
options = ['left', 'center', 'right']
fig, axs = plt.subplots(len(options), 1, constrained_layout=True)

for ax, loc in zip(axs, options):
    ax.plot([1, 2, 3])
    ax.set_xlabel(f'xlabel loc={loc!r}', loc=loc)

# Right plot
options = ['bottom', 'center', 'top']
fig, axs = plt.subplots(1, len(options), constrained_layout=True)

for ax, loc in zip(axs, options):
    ax.plot([1, 2, 3])
    ax.set_ylabel(f'ylabel loc={loc!r}', loc=loc)

这篇关于如何在matplotlib中将轴标签对齐到右侧或顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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