gpt4 book ai didi

python - 如何在 Python Seaborn 的线图中的每个标记上添加值/标签?

转载 作者:行者123 更新时间:2023-12-05 03:51:30 25 4
gpt4 key购买 nike

我有一个数据框,其中包含时间范围、股票和文本计数作为列。数据框看起来像这样

              Time Stock  Text
0 00:00 - 01:00 BBNI 371
1 00:00 - 01:00 BBRI 675
2 00:00 - 01:00 BBTN 136
3 00:00 - 01:00 BMRI 860
4 01:00 - 02:00 BBNI 936
5 01:00 - 02:00 BBRI 1325
6 01:00 - 02:00 BBTN 316
7 01:00 - 02:00 BMRI 1630

我想用下面的代码画一个线图:

df=tweetdatacom.groupby(["Time","Stock"])[["Text"]].count().reset_index()
plt.figure(figsize=(10,5))
line=sn.lineplot(x="Time", y="Text",hue="Stock",palette=["green","orange","red","blue"],marker="o",data=df)
plt.xticks(size=5,rotation=45, horizontalalignment='right',fontweight='light',fontsize='large')
plt.xlabel('Time Interval',size=12)
plt.ylabel('Total Tweets',size=12)

这是我用代码得到的结果: enter image description here

现在,我想将每个标记的值放在绘图上,我该怎么做?

非常感谢

最佳答案

使用 ax.text 遍历数据的数量。我仅使用提供给我的数据创建此内容,因此我省略了您的一些处理过程。

import pandas as pd
import numpy as np
import io

data = '''
Time Stock Text
0 "00:00 - 01:00" BBNI 371
1 "00:00 - 01:00" BBRI 675
2 "00:00 - 01:00" BBTN 136
3 "00:00 - 01:00" BMRI 860
4 "01:00 - 02:00" BBNI 936
5 "01:00 - 02:00" BBRI 1325
6 "01:00 - 02:00" BBTN 316
7 "01:00 - 02:00" BMRI 1630
'''

df = pd.read_csv(io.StringIO(data), sep='\s+')
import seaborn as sn
import matplotlib.pyplot as plt
# df=tweetdatacom.groupby(["Time","Stock"])[["Text"]].count().reset_index()

plt.figure(figsize=(10,5))

palette = ["green","orange","red","blue"]
line=sn.lineplot(x="Time", y="Text",hue="Stock",palette=palette, marker="o", data=df)

plt.xticks(size=5,rotation=45, horizontalalignment='right',fontweight='light',fontsize='large')
plt.xlabel('Time Interval',size=12)
plt.ylabel('Total Tweets',size=12)

for item, color in zip(df.groupby('Stock'),palette):
for x,y,m in item[1][['Time','Text','Text']].values:
# print(x,y,m)
plt.text(x,y,m,color=color)

enter image description here

关于python - 如何在 Python Seaborn 的线图中的每个标记上添加值/标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62856279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com