python - matplotlib文本框中多行文本的水平对齐
问题描述
我想在一个右对齐的框中左对齐多行文本,或者更确切地说,其位置是根据其右边缘定义的。
也就是说,我想要文本“其他”。在以下要左对齐:
import matplotlib.pyplot as plt
def lowerrighttext(ax,text, size=12, alpha = 0.5, color='k', dx=1.0/20, dy=1.0/20):
return ax.annotate(text, xy=(1-dx, dy), xycoords = 'axes fraction',
ha='right',va='bottom', color=color,
size=size, bbox=dict(boxstyle="round", fc="w",
ec="0.5", alpha=alpha) )
fig,ax=plt.subplots(1)
ax.plot([0,1],[0,1])
lowerrighttext(ax,'One line is longer than\nthe other.')
plt.show()
解决方案
看起来诀窍是multialignment
orma
命名的参数:
import matplotlib.pyplot as plt
def lowerrighttext(ax,text, size=12, alpha = 0.5, color='k', dx=1.0/20, dy=1.0/20):
return ax.annotate(text, xy=(1-dx, dy), xycoords = 'axes fraction',
ha='right',va='bottom',ma='left',color=color, # Here
size=size, bbox=dict(boxstyle="round", fc="w",
ec="0.5", alpha=alpha))
fig,ax=plt.subplots(1)
ax.plot([0,1],[0,1])
lowerrighttext(ax,'One line is longer than\nthe other.')
plt.show()
产生:
(虽然看起来还是有点偏,与底部和右侧的距离不等,但可能是身材宽大于高的影响。)
推荐阅读
- spring-boot - 如何在子类 JPA 中覆盖超类的映射列的类型
- python-3.x - 我的网页抓取工具一直有效,直到我将它与 for 循环一起使用
- javascript - 使用 Array 方法`.reduce()`,请编写循环遍历对象数组并返回分析的逻辑
- php - 生产中的 Laravel - Docker
- wordpress - 如何使用 LearnDash 插件创建课程播放列表?
- fp-ts - fp-ts:如何展平 ReaderTaskEither 数组的一层?
- excel - 除法公式相加
- verilog - RTL if 语句与模拟不匹配
- ios - 如何生成逐渐改变颜色的调色板
- javascript - 带有 sinon 的 vue 模拟类用于单元测试