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

LonelySoul
  • 1,212
  • 5
  • 18
  • 45

2 Answers2

26

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.

TomAugspurger
  • 28,234
  • 8
  • 86
  • 69
  • Thanks for both the answer. However how to put name of "subid" with the dots. – LonelySoul Jul 06 '13 at 22:45
  • 1
    http://stackoverflow.com/questions/15910019/annotate-data-points-while-plotting-from-pandas-dataframe/15911372#15911372 – Dan Allan Jul 07 '13 at 13:37
  • `matplotlib.pyplot`. He probably started ipython with the `--pylab` flag which imports that (and a bunch of other stuff) automatically. – TomAugspurger Jul 09 '13 at 13:07
  • 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. – Dan Allan Jul 09 '13 at 15:49
  • n.b. `df.sort()` is now deprecated for [`df.sort_values()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_values.html) – Bruce Jul 18 '22 at 05:23
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()
Nilani Algiriyage
  • 32,876
  • 32
  • 87
  • 121