用 matplotlib 注释来自 Pandas 数据帧中的点


要使用 Matplotlib 注释来自 Pandas 数据帧中的点,我们可以采用以下步骤 -

  • 设置图形大小并调整子图之间的填充和周围填充。

  • 制作一个满足 x、ytextc 列的二维、尺寸可变、可能异构的表格数据。

  • 使用 plot() 方法绘制列 xy 数据点。

  • 使用可选的轴间集合逻辑将 Pandas 对象沿着特定轴连接起来。

  • 迭代 Pandas 对象。

  • 使用 text() 方法放置每个绘制点上的文本。

  • 要显示图形,使用 show() 方法。

示例

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame(dict(x=[2, 4, 1, 5, 2], y=[2, 1, 3, 5, 7], text=['First', 'Second', 'Third', 'Fourth', 'Fifth']))

ax = df.set_index('x')['y'].plot(style='*', color='red', ms=20)

a = pd.concat({'x': df.x, 'y': df.y, 'text': df.text}, axis=1)

for i, point in a.iterrows():
   ax.text(point['x']+0.125, point['y'], str(point['text']))

plt.show()

输出

更新于: 2021 年 8 月 9 日

1 千+ 浏览量

开启你的 职业

通过完成课程获得认证

开始
广告
© . All rights reserved.