我的情节中的标记距离图像中的位置很远

发布于 2025-01-30 18:38:14 字数 706 浏览 1 评论 0原文

我有以下代码,该代码绘制了一个普通的plt.plot,每个值都在每个值中绘制标记,但是标记文本远离图像中的标记点。我能做什么?这取决于图的大小吗?因为我也尝试了不同的数字尺寸。

import matplotlib.pyplot as plt

df.reset_index(inplace=True)

f = plt.figure()
f.set_figwidth(10)
f.set_figheight(8)


x = df["State"]
y = df["Total revenue"]


def add_value_label(x_list,y_list):
    for i in range(1, len(x_list)+1):
        plt.text(i,y_list[i-1],y_list[i-1])

add_value_label(x, y)

plt.plot(x, y, marker='o',color ='orange',markerfacecolor='orange',markeredgecolor='orange')



plt.show()

I have the following code, which plots a normal plt.plot with markers in each value, however the markers text is far away from the marker point as seen in the image. Is there something I can do? Does it depends in the figure size? because I also tried with different figure sizes.

Plot image

import matplotlib.pyplot as plt

df.reset_index(inplace=True)

f = plt.figure()
f.set_figwidth(10)
f.set_figheight(8)


x = df["State"]
y = df["Total revenue"]


def add_value_label(x_list,y_list):
    for i in range(1, len(x_list)+1):
        plt.text(i,y_list[i-1],y_list[i-1])

add_value_label(x, y)

plt.plot(x, y, marker='o',color ='orange',markerfacecolor='orange',markeredgecolor='orange')



plt.show()

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-02-06 18:38:14

您正在计算从1的x值和0的y值。您可以在图中看到,这些值是正确的,但是它们将一个X列移到右侧。

def add_value_label(x_list,y_list):
    for i,xv in enumerate(x_list):
        plt.text(xv,y_list[i],y_list[i])

You're counting your X values from 1 and your Y values from 0. You can see that in the plot -- the values are right, but they're shifted one x column to the right.

def add_value_label(x_list,y_list):
    for i,xv in enumerate(x_list):
        plt.text(xv,y_list[i],y_list[i])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文