0

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?

1 Answer 1

2

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

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

1 Comment

Thank you for the answer! For python3 I was required to change xrange(1, 7) to range(1, 7).

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.