简体   繁体   English

如何避免使用 Matplotlib 旋转后文本倒置?

[英]How to avoid text being upside down after rotation with Matplotlib?

I am trying to place labels on an image with Matplotlib. I need to place labels in eight octilinear directions around a node, for which I am currently using the text function with rotation mode anchor .我正在尝试在带有 Matplotlib 的图像上放置标签。我需要在节点周围的八个八线方向放置标签,为此我目前正在使用text function 和旋转模式anchor

However, with some angles, eg 180°, the label is left of the node as I want it to be, but the text is upside down as a result of the rotation.然而,对于某些角度,例如 180°,label 位于节点的左侧,正如我希望的那样,但文本由于旋转而颠倒了。 My workaround so far is to flip the text again using the upsidedown library:到目前为止,我的解决方法是使用upsidedown库再次翻转文本:

ax.text(x + x_fac * SCALE / 10.0, y + y_fac * SCALE / 10.0, upsidedown.transform(stop_label),
        rotation=angle, rotation_mode='anchor')

However, some characters are not displayed correctly when flipped.但是,翻转时某些字符无法正确显示。 This is why I am searching for a better way to do this.这就是为什么我正在寻找一种更好的方法来做到这一点。 Does someone have an idea?有人有想法吗?

Check out horizontalalignment .检查horizontalalignment 预览图片

ax.text(0.5, 0.5, "Right", horizontalalignment="right", rotation=45, rotation_mode="anchor")
ax.text(0.5, 0.5, "Left", horizontalalignment="left", rotation=315, rotation_mode="anchor")

When right -aligned, text will be left of the node, so 180° become 0°.对齐时,文本将位于节点的左侧,因此 180° 变为 0°。 There is no need for upsidedown .没有必要upsidedown

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM