/ /複数の列からのデータに基づいてPandas Pythonで1つの図に複数の線をプロットする方法 - python、pandas、matplotlib、plot

複数の列のデータに基づいてPandas Pythonで複数の行を1つの図にプロットする方法は? - python、pandas、matplotlib、plot

このように、3列のデータフレームがあります。

df["year"] = ["2005, 2005, 2005, 2015, 2015, 2015, 2030, 2030, 2030"]
df["name"] = ["A", "B", "C", "A", "B", "C", "A", "B", "C"]
df["weight"] = [80, 65, 88, 65, 60, 70, 60, 55, 65]

どうやってA、B、Cの線を描くことができますか。ここでは、年を経て体重がどのように伸びていくかを示しています。だから私はこれを試してみました:

df.groupby("euro").plot(x="year", y="MKM")

しかし、私は複数のプロットを得ています、そしてそれは私が欲しいものではありません。これらすべてのプロットを1つの図にまとめたい。

回答:

回答№1は1

これはあなたが探しているものを生み出しますか?

import matplotlib.pyplot as plt
fig,ax = plt.subplots()

for name in ["A","B","C"]:
ax.plot(df[df.name==name].year,df[df.name==name].weight,label=name)

ax.set_xlabel("year")
ax.set_ylabel("weight")
ax.legend(loc="best")

ここに画像の説明を入力