从pandas数据框添加文本注释到matplotlib图

时间:2016-06-06 15:36:43

标签: python pandas matplotlib

如何添加文本注释并将绘制的点和数值标记到pandas数据帧的matplotlib图中?数据帧的列不是固定大小,因不同文件而异。

dataframe = pd.read_csv('file1.csv')

plt.figure(figsize=(50,25))
dataframe.plot()
plt.xticks(rotation=45, fontsize=8)
plt.yticks(fontsize=8)

1 个答案:

答案 0 :(得分:5)

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.DataFrame({'x':range(0, 100, 20),
                   'y':np.random.randint(0,100,5)})

rows, cols = df.shape
fig, ax = plt.subplots(figsize=(50/10,25/10))
df.plot(ax=ax)

for col in range(cols):
    for i in range(rows):
        ax.annotate('{}'.format(df.iloc[i, col]), xy=(i, df.iloc[i, col])) 
plt.xticks(rotation=45, fontsize=8)
plt.yticks(fontsize=8)

plt.show()

enter image description here