Search code examples
pythonggplot2python-ggplot

Get python ggplot bar axis right?


I want to plot the results of throwing a dice in a bar chart using ggplot.

import numpy
import ggplot
import pandas

frame = pandas.DataFrame()

for i in range(6):
    frame.loc[i, 'pips'] = i+1

rand = numpy.random.random_integers(1, 6, 100)

for i in range(len(count)):
    frame.loc[i, 'events'] = numpy.sum(rand == i+1)

frame['propability'] = frame.events / frame.events.sum()

ggplot.ggplot(ggplot.aes(x='pips', weight='events'), frame) + ggplot.geom_bar()

The outcome is this plot

From the examples I expected the plot to

  1. Have more width on each bar
  2. Not have x=7 with no bar

How do I fix these two things?


Solution

  • Use parameters "binwidth", "limits" and "breaks" like this :

     ggplot.ggplot(ggplot.aes(x='pips', weight='events'), frame) +
     ggplot.geom_bar(binwidth=1) +
     ggplot.scale_x_continuous(limits = (1,6), breaks = range(1,7))
    

    Which gives me :

    Histogram