熊猫散点图和groupby不起作用

时间:2019-12-10 13:48:13

标签: pandas scatter-plot

我正在尝试对熊猫进行散点图。不幸的是kind='scatter'不起作用。如果我将其更改为kind='line',它将按预期工作。我该怎么做才能解决此问题?

for label, d in df.groupby('m'):
    d[['te','n']].sort_values(by='n', ascending=False).plot(kind="scatter", x='n', y='te', ax=ax, label='m = '+str(label))```

1 个答案:

答案 0 :(得分:0)

改为使用plot.scatter

df = pd.DataFrame({'x': [0, 5, 7,3, 2, 4, 6], 'y': [0, 5, 7,3, 2, 4, 6]})
df.plot.scatter('x', 'y')

Output

如果要使用单独的标签和颜色,请使用以下代码段:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'm': np.random.randint(0, 5, size=100),
    'x': np.random.uniform(size=100),
    'y': np.random.uniform(size=100),
})

fig, ax = plt.subplots()
for label, d in df.groupby('m'):
    # generate a random color:
    color = list(np.random.uniform(size=3))
    d.plot.scatter('x', 'y', label=f'group {label}', ax=ax, c=[color])

Different colours and labels