エラーバーしたい(pandas.DataFrame.plot)
1import pandas as pd
2import matplotlib.pyplot as plt
3
4# データを準備する
5data = pd.DataFrame({
6 "x": [1, 2, 3, 4, 5],
7 "y": [2, 3, 5, 7, 11],
8 "xerr": [0.1, 0.2, 0.1, 0.3, 0.2],
9 "yerr": [0.2, 0.3, 0.2, 0.4, 0.3],
10});
11
12# 散布図を描く
13# ax: matplotlib.axes.Axes
14ax = data.plot.scatter(
15 x="x",
16 y="y",
17)
18
19# エラーバーを描く
20ax.errorbar(
21 x=data["x"],
22 y=data["y"],
23 xerr=data["xerr"],
24 yerr=data["yerr"],
25 fmt='o', # マーカーのスタイル
26 capsize=3,
27)
28
29# グラフのタイトルと軸ラベルを設定
30ax.set_title("エラーバー付きの散布図")
31ax.set_xlabel("X軸")
32ax.set_ylabel("Y軸")
33
34# グラフを表示
35plt.show()
pd.DataFrame.plot単体ではエラーバーを描くことができません。
そのためpd.DataFrame.plot.scatterで散布図を描いた後に、Axes.errorbarでエラーバーを描きます。