Python/Matplotlib - 如何将文本放在等宽图形的角落 [英] Python/Matplotlib - How to put text in the corner of equal aspect figure

查看:29
本文介绍了Python/Matplotlib - 如何将文本放在等宽图形的角落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在等宽图的右下角放置文本.我通过 ax.transAxes 设置相对于图形的位置,但是我必须根据每个图形的高度比例手动定义相对坐标值.

I would like to put text in the right bottom corner of equal aspect figure. I set the position relative to the figure by ax.transAxes, but I have to define the relative coordinate value manually depending on height scales of each figures.

了解轴高度比例和脚本中正确文本位置的好方法是什么?

What would be a good way to know axes height scale and the correct text position within the script?

 ax = plt.subplot(2,1,1)
 ax.plot([1,2,3],[1,2,3])
 ax.set_aspect('equal')
 ax.text(1,-0.15, 'text', transform=ax.transAxes, ha='right', fontsize=16)
 print ax.get_position().height

 ax = plt.subplot(2,1,2)
 ax.plot([10,20,30],[1,2,3])
 ax.set_aspect('equal')
 ax.text(1,-0.15, 'text', transform=ax.transAxes, ha='right', fontsize=16)
 print ax.get_position().height                                              

推荐答案

使用注释.

事实上,我几乎从不使用text.即使我想在数据坐标中放置东西,我通常也希望以点为单位将其偏移一些固定距离,使用 annotate 会更容易.

In fact, I hardly ever use text. Even when I want to place things in data coordinates, I usually want to offset it by some fixed distance in points, which is much easier with annotate.

作为一个简单的例子:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, subplot_kw=dict(aspect=1))

axes[0].plot(range(1, 4))
axes[1].plot(range(10, 40, 10), range(1, 4))

for ax in axes:
    ax.annotate('Test', xy=(1, 0), xycoords='axes fraction', fontsize=16,
                horizontalalignment='right', verticalalignment='bottom')
plt.show()

如果您希望它稍微偏离角落,您可以通过 xytext kwarg(和 textcoords 来控制 的值如何指定偏移量xytext 进行解释).我也在此处使用 hava 的缩写来表示 horizo​​ntalalignmentverticalalignment:

If you'd like it slightly offset from the corner, you can specify an offset through the xytext kwarg (and textcoords to control how the values of xytext are interpreted). I'm also using the ha and va abbreviations for horizontalalignment and verticalalignment here:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, subplot_kw=dict(aspect=1))

axes[0].plot(range(1, 4))
axes[1].plot(range(10, 40, 10), range(1, 4))

for ax in axes:
    ax.annotate('Test', xy=(1, 0), xycoords='axes fraction', fontsize=16,
                xytext=(-5, 5), textcoords='offset points',
                ha='right', va='bottom')
plt.show()

如果您尝试将其放置在轴下方,您可以使用偏移量将其放置在以点为单位的下方设置距离处:

If you're trying to place it below the axes, you can use the offset to place it a set distance below in points:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, subplot_kw=dict(aspect=1))

axes[0].plot(range(1, 4))
axes[1].plot(range(10, 40, 10), range(1, 4))

for ax in axes:
    ax.annotate('Test', xy=(1, 0), xycoords='axes fraction', fontsize=16,
                xytext=(0, -15), textcoords='offset points',
                ha='right', va='top')
plt.show()

另请查看 Matplotlib 注释指南以了解更多信息.

Also have a look at the Matplotlib annotation guide for more information.

这篇关于Python/Matplotlib - 如何将文本放在等宽图形的角落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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