7

Given this DataFrame:

from pandas import DataFrame
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo'], ['one', 'two', 'one', 'two',        'one', 'two']]

tuples = zip(*arrays)

index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])

df = DataFrame(randn(3, 6), index=[1, 2, 3], columns=index)

How can I plot a chart with: X-axis: 1,2,3. The three series names are: bar, baz, foo. Y-axis values: 'one' column. The label next to each dot is the 'two' column.

So, in other words, say I have three stocks (bar, baz, & foo), with each of them having its respective stock price ('one') for each date (1,2,3), and the comment for each dot is at the 'two' column. How can I chart that?

(Sorry for not showing the df table, I don't know how to copy it correctly)

erantdo
  • 685
  • 2
  • 9
  • 19
  • What part of this you can't do? Select part of data? Plot? Plot with labels? Do you know how to plot this chart (without labels)? Can you provide some of your (may be partially working) attempts. – alko Dec 13 '13 at 16:03
  • Hi, thanks for commenting. No, I can't chart this DataFrame without labels, I can't chart it at all – erantdo Dec 13 '13 at 16:43
  • Meaning, I don't know how to select the data (I know how to plot in general) – erantdo Dec 13 '13 at 16:44

1 Answers1

12

Start with dataframe of form

>>> df
first        bar                 baz                 foo
second       one       two       one       two       one       two
1       0.085930 -0.848468  0.911572 -0.705026 -1.284458 -0.602760
2       0.385054  2.539314  0.589164  0.765126  0.210199 -0.481789
3      -0.352475 -0.975200 -0.403591  0.975707  0.533924 -0.195430

Select and plot 'one' column

>>> one = df.xs('one', level=1, axis=1)
>>> one
first       bar       baz       foo
1      0.085930  0.911572 -1.284458
2      0.385054  0.589164  0.210199
3     -0.352475 -0.403591  0.533924    

>>> pyplot.show(one.plot())

enter image description here

alko
  • 46,136
  • 12
  • 94
  • 102
  • Thanks a lot. Any idea how can I add the 'two' as notes next to each dot? and what if I want another column, say 'three' to also be included as a note in addition to 'two' ? – erantdo Dec 13 '13 at 18:27
  • 1
    @erantdo In fact, I have one possible solution in mind, but dislike answering several questions in one, so choosed to answer your main concern. You can try it yourself, consulting with http://stackoverflow.com/questions/5147112/matplotlib-how-to-put-individual-tags-for-a-scatter-plot, and If you'll find yoursef stuck, start a new topic (at least, show that you tried). – alko Dec 13 '13 at 18:30