ax.plot https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.plot.html不具有x
and y
论据。
签名是Axes.plot(*args, **kwargs)
, 意思是x
and y
只是位置参数。如果您指定x=
and y=
它们将被视为关键字参数并被忽略。
所以删除x=
and y=
从代码来看,
ax.plot(group.iloc[:,x], group.iloc[:,y], marker='o', linestyle='',label=name)
完整示例:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({"x":np.random.rand(40),
"y":np.random.rand(40),
"category": np.random.choice(list("ABCD"), size=40)})
category = "category"
x=1; y=2
groups = df.groupby(category)
fig, ax = plt.subplots()
for name, group in groups:
ax.plot(group.iloc[:,x], group.iloc[:,y], marker='o', linestyle='',label=name)
fig = ax.get_figure()
#fig.savefig(path)
plt.show()