我希望在右对齐的框中左对齐多行文本,或者更确切地说,其位置是根据其右边缘定义的。
也就是说,我想要文本“the other”。下面的内容要左对齐:
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
or ma
命名参数:
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()
产品:
(虽然看起来还是有点偏离,底部和右侧的距离不等,但这可能是图形宽度大于高度的效果。)
关于python - matplotlib 文本框中多行文本的水平对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50889973/