Python 使用pandas和matplotlib进行绘图
我正在尝试用Python创建散点图。我有一个具有指定类别的数据帧“df”,x和y是列号:Python 使用pandas和matplotlib进行绘图,python,pandas,matplotlib,dataframe,scatter-plot,Python,Pandas,Matplotlib,Dataframe,Scatter Plot,我正在尝试用Python创建散点图。我有一个具有指定类别的数据帧“df”,x和y是列号: groups = df.groupby(category) fig, ax = plt.subplots() for name, group in groups: ax.plot(x=group.iloc[:,x], y=group.iloc[:,y], marker='o', linestyle='',label=name) fig = ax.get_figure() fig.savefig(pa
groups = df.groupby(category)
fig, ax = plt.subplots()
for name, group in groups:
ax.plot(x=group.iloc[:,x], y=group.iloc[:,y], marker='o', linestyle='',label=name)
fig = ax.get_figure()
fig.savefig(path)
由于某种原因,我得到了一个空的散点图——我做错了什么吗 没有x
和y
参数
签名是Axes.plot(*args,**kwargs),这意味着x
和y
只是位置参数。如果指定x=
和y=
它们将被视为关键字参数并被忽略
因此,从代码中删除x=
和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()
谢谢你的帮助:)