0

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?

2
  • Your code ran without error for me. Commented May 10, 2017 at 17:13
  • did the colors, red and green, appear? Commented May 10, 2017 at 17:15

1 Answer 1

4

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.