将数据标签添加到折线图 [英] Adding data labels to linechart

查看:71
本文介绍了将数据标签添加到折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 pandas/matplotlib 在此图的圆圈顶部添加数据标签.

数据是从 Excel 文件中加载的,使用 Pandas 和 'Month' 作为索引

#导入数据路径 = 'xyz.xlsx'df = pd.read_excel(Path,sheetname=0,index_col='Month')

然后我继续使用以下代码绘制数据

plt.plot(df['Sales1'],marker='o',label='Sales')plt.show()

我尝试过注释,但无法使其正常工作.

我的数据框看起来像这样

 Sales1 Sales2 Sales3月2015-08-01 24457 31895 420812015-09-01 6926 43584 206662015-10-01 4973 4845 109622015-11-01 21345 17909 361152015-12-01 8639 40668 382152016-01-01 48021 18145 253532016-02-01 6708 24651 460892016-03-01 8827 18617 312152016-04-01 49703 14205 269832016-05-01 3223 16658 18542016-06-01 6484 46503 135232016-07-01 41243 18876 207402016-08-01 21779 13362 489972016-09-01 9494 40242 154772016-10-01 1205 10291 326632016-11-01 42655 41375 485492016-12-01 24644 26002 66022017-01-01 33732 44292 451512017-02-01 47880 15503 14042017-03-01 32499 17755 111352017-04-01 42888 31527 251742017-05-01 34433 8292 201172017-06-01 9884 2359 454242017-07-01 35302 24177 48045

解决方案

不完全确定您尝试了什么,但 annotate 应该可以工作.类似于

I would like to add data labels on top of the circles of this graph with pandas/matplotlib.

The data is loaded from an Excel file using pandas and 'Month' as index

#Importing the data
Path = 'xyz.xlsx'
df = pd.read_excel(Path,sheetname=0,index_col='Month')

I then proceed to plot the data using the following code

plt.plot(df['Sales1'],marker='o',label='Sales')
plt.show()

I have tried annotating, but can't get it to work.

my dataframe looks like this

           Sales1  Sales2  Sales3
Month                             
2015-08-01   24457   31895   42081
2015-09-01    6926   43584   20666
2015-10-01    4973    4845   10962
2015-11-01   21345   17909   36115
2015-12-01    8639   40668   38215
2016-01-01   48021   18145   25353
2016-02-01    6708   24651   46089
2016-03-01    8827   18617   31215
2016-04-01   49703   14205   26983
2016-05-01    3223   16658    1854
2016-06-01    6484   46503   13523
2016-07-01   41243   18876   20740
2016-08-01   21779   13362   48997
2016-09-01    9494   40242   15477
2016-10-01    1205   10291   32663
2016-11-01   42655   41375   48549
2016-12-01   24644   26002    6602
2017-01-01   33732   44292   45151
2017-02-01   47880   15503    1404
2017-03-01   32499   17755   11135
2017-04-01   42888   31527   25174
2017-05-01   34433    8292   20117
2017-06-01    9884    2359   45424
2017-07-01   35302   24177   48045

解决方案

Not exactly sure what you tried but annotate should work. Something similar to the solution discussed here should do the trick

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(pd.np.random.randn(5,1)*10 + 100,
                  index=pd.date_range("2015-01-01", "2015-01-05"),
                  columns=["Sales1"]).round()

fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(df['Sales1'], marker='o', label='Sales')

for i,j in df.Sales1.items():
    ax.annotate(str(j), xy=(i, j))

plt.show()

这篇关于将数据标签添加到折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆