10

I am new to matplotlib and I am trying to use it within pandas to plot some simple charts. I have a DataFrame that contains two labels "score" and "person", derived from another DF.

df1 = DataFrame(df, columns=['score','person'])

Producing this output:

table output

I am trying to create a simple bar chart, to show each person in different color, and this is what I have thus far:

df1.plot(kind='bar', title='Ranking')

bar chart

How can I customize it so the chart shows the person names in the x axis with unique colors and remove the "frame" surrounding the figure? How can I make it a horizontal bar chart?

Thanks in advance for your help.

Luis Miguel
  • 5,057
  • 8
  • 42
  • 75
  • 7
    i just want to say i love your drawing skill and would pay money to output my charts like that – swyx Jun 12 '16 at 09:19
  • 2
    @swyx: these are created by matplotlib feature: *[xkcd-style plots](http://matplotlib.org/xkcd/examples/showcase/xkcd.html)*, inspired in turn by [this question on Mathematica.SX](https://mathematica.stackexchange.com/questions/11350/xkcd-style-graphs). – ojdo May 29 '17 at 12:13
  • @ojdo thanks! I meant to clarify that a while ago – Luis Miguel May 29 '17 at 14:50

2 Answers2

13

I guess this will give you the idea:

df = pd.DataFrame({'score':np.random.randn(6),
                   'person':[x*3 for x in list('ABCDEF')]})

ax = plt.subplot(111)
df.score.plot(ax=ax, kind='barh', color=list('rgbkym'), title='ranking')
ax.axis('off')
for i, x in enumerate(df.person):
    ax.text(0, i + .5, x, ha='right', fontsize='large')

for

  person  score
0    AAA   1.79
1    BBB   0.31
2    CCC  -0.52
3    DDD   1.59
4    EEE   0.59
5    FFF  -1.03

you will get:

hbar

behzad.nouri
  • 74,723
  • 18
  • 126
  • 124
  • Thank you. How would you rank the order and show the values for each bar? (i.e. 1.79 for AAA in the first position from top to bottom, DDD in second position, etc.?). – Luis Miguel Dec 15 '13 at 23:06
  • @LuisMiguel you can use `ax.text( x, y, string )` to add extra text to the plot at position (x, y). the order is the same as in the series/data-frame – behzad.nouri Dec 15 '13 at 23:16
  • 2
    @LuisMiguel this might help: http://stackoverflow.com/questions/19917587/matplotlib-advanced-bar-plot/19919397#19919397 – Paul H Dec 16 '13 at 21:31
-2

Check out the docs for pd.DataFrame.plot() for more info. You'll also definitely want to read up on plotting with matplotlib as well as the matplotlib docs themselves.

Getting a horizontal bar graph is easy, just use `kind='barh'.

MattDMo
  • 100,794
  • 21
  • 241
  • 231