16

Looks simple but I am not able to draw a X-Y chart with "dots" in pandas DataFrame. I want to show the subid as "Mark" on X Y Chart with X as age and Y as fdg .

Code so far

mydata = [{'subid': 'B14-111', 'age': 75, 'fdg': 3}, {'subid': 'B14-112', 'age': 22, 'fdg': 2}, {'subid': 'B14-112', 'age': 40, 'fdg': 5}]

df = pandas.DataFrame(mydata)

DataFrame.plot(df,x="age",y="fdg")

show()

enter image description here

2 Answers 2

27

df.plot() will accept matplotlib kwargs. See the docs

mydata = [{'subid': 'B14-111', 'age': 75, 'fdg': 3}, {'subid': 'B14-112', 'age': 22, 
           'fdg': 2}, {'subid': 'B14-112', 'age': 40, 'fdg': 5}]

df = pandas.DataFrame(mydata)
df = df.sort(['age'])  # dict doesn't preserve order
df.plot(x='age', y='fdg', marker='.')

enter image description here

Reading your question again, I'm thinking you might actually be asking for a scatterplot.

import matplotlib.pyplot as plt
plt.scatter(df['age'], df['fdg'])

Have a look at the matplotlib docs.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for both the answer. However how to put name of "subid" with the dots.
matplotlib.pyplot. He probably started ipython with the --pylab flag which imports that (and a bunch of other stuff) automatically.
Correct. Once you use ipython --pylab, you'll never go back. It's bad practice to import pylab globally into a module, but when I'm working interactively I think it's dead useful.
n.b. df.sort() is now deprecated for df.sort_values()
4

Try following for a scatter diagram.

import pandas
from matplotlib import pyplot as plt

mydata = [{'subid': 'B14-111', 'age': 75, 'fdg': 3}, {'subid': 'B14-112', 'age': 22, 
           'fdg': 2}, {'subid': 'B14-112', 'age': 40, 'fdg': 5}]

df = pandas.DataFrame(mydata)
x,y = [],[]

x.append (df.age)
y.append (df.fdg)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(y,x,'o-')
plt.show()

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.