M.M
M.M

Reputation: 1373

plotting pandas points with different colors

I have a pandas data frame of two columns ['frequency','color'] and it looks like this:

 name  frequency  color
0   351   r
1   122   r
2   30    g
3   85    r
4   195   r
5   88    g
6   130   r
7   85    r
8   41    r
9   9     g

I want to plot the 'frequency' sorted and depending on the colors. I tried this:

plt.scatter(y=np.sort(data['frequency']),x=range(len(data['frequency'])),c=np.sort(data['color']))

and i got the following error:

ValueError: to_rgba: Invalid rgba arg "['r']" to_rgb: Invalid rgb arg "('r',)" sequence length is 1; must be 3 or 4

what is wrong in the code?

Upvotes: 0

Views: 3765

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

The following figure
enter image description here

is produced by this code:

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

frequency = [351,122,30,85,195,88,130,85,41,9]
color = ["r","r","g","r","r","g","r","r","r","g"]
df = pd.DataFrame( {"frequency" : frequency, "color" : color})
df.sort_values("frequency", inplace=True)

plt.scatter(x=range(len(df)), y= df["frequency"], c = df["color"])
plt.show()

Upvotes: 4

Related Questions