猿问

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

我正在尝试使用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)

MYYA
浏览 224回答 1
1回答

繁花不似锦

仅注释要注释的值。在您的情况下,y值?# Label the data points here.for xy in zip(helper, ageList):    plt.annotate('(%s)' % xy[1], xy=xy, textcoords='data'
随时随地看视频慕课网APP

相关分类

Python
我要回答