使用Python在Excel中标记单个变量数据点

时间:2018-06-30 01:52:03

标签: python matplotlib

我正在尝试使用Python 2.7,matplotlib和xlwings在Excel中绘制图表。 我有以下图表,但我只想标记年龄而不是年龄和助手列。

我该怎么做?

数据

Helper  Age
0   9
1   30
2   27
3   40
4   45
5   56
6   44
7   21
8   45
9   45

代码

import pandas as pd
import matplotlib.pyplot as plt
import xlwings as xw

# Data table
df = pd.read_excel("test.xlsx", "Sheet2")


# My workbook.
wb = xw.Book('test.xlsx')

# Instantiate the worksheet.
sht2 = wb.sheets["Sheet2"]

# Dump Age column into a dataframe.
ageList = df['Age'].values.tolist()
helper = df['Helper'].values.tolist()


fig = plt.figure()

# Line plot.
plt.plot(ageList, ls = '--', color = 'red')

# Label the data points here.
for xy in zip(helper, ageList):
    plt.annotate('(%s, %s)' % xy, xy=xy, textcoords='data')

sht2.pictures.add(fig, name = 'TestPlot', update = True)

图表

enter image description here

1 个答案:

答案 0 :(得分:0)

仅注释您要注释的值。在您的情况下,y值是

# Label the data points here.
for xy in zip(helper, ageList):
    plt.annotate('(%s)' % xy[1], xy=xy, textcoords='data'