matplotlib注释向右移动

发布于 2025-02-05 19:13:54 字数 890 浏览 1 评论 0原文

我正在绘制线路上的一条线和一些散点点。我想标记线上散点点的X坐标,并将注释被线阻塞。如果没有手动输入注释的位置,是否有一种方法可以将所有注释文本转移到右侧?

下面是产生散点图和注释的代码的一部分。

import numpy as np
import matplotlib.pyplot as plt
fs = 20
figure, ax1 = plt.subplots()
ax1.set_xlim(1.6, 2.0)
ax1.set_ylim(-2, 5)
annotations = ['$1.66$', '$1.68$', '$1.72$', '$1.74$', '$1.76$']
xpoint = [1.66, 1.68, 1.72, 1.74, 1.76]
ypoint = [3.47072347e-01, 5.05186795e+00, 1.61807901e+01, 5.60855544e+01, 6.07027325e+02]
ax1.scatter(xpoint, np.log10(ypoint), color='k')
for i, label in enumerate(annotations):
    ax1.annotate(label, (xpoint[i], np.log10(ypoint[i])), ha='left', va='center', fontsize=fs)

ax1.tick_params(labelsize=fs)
figure.savefig("plot.svg", bbox_inches='tight')

I'm plotting both a line and some scatter points on the line. I want to label the x-coordinate of the scatter points on the line, and the annotations are being obstructed by the line. Without manually inputting the position of the annotations, is there a way to shift all of the annotation texts to the right by a certain amount?

Below is part of the code to produce the scatter plot and annotation.

import numpy as np
import matplotlib.pyplot as plt
fs = 20
figure, ax1 = plt.subplots()
ax1.set_xlim(1.6, 2.0)
ax1.set_ylim(-2, 5)
annotations = ['$1.66

enter image description here

, '$1.68

enter image description here

, '$1.72

enter image description here

, '$1.74

enter image description here

, '$1.76

enter image description here

] xpoint = [1.66, 1.68, 1.72, 1.74, 1.76] ypoint = [3.47072347e-01, 5.05186795e+00, 1.61807901e+01, 5.60855544e+01, 6.07027325e+02] ax1.scatter(xpoint, np.log10(ypoint), color='k') for i, label in enumerate(annotations): ax1.annotate(label, (xpoint[i], np.log10(ypoint[i])), ha='left', va='center', fontsize=fs) ax1.tick_params(labelsize=fs) figure.savefig("plot.svg", bbox_inches='tight')

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文