Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6677695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:09:12+00:00 2026-05-26T04:09:12+00:00

Working in Python 2.7. I’m trying to plot a histogram for the numbers generated

  • 0

Working in Python 2.7.

I’m trying to plot a histogram for the numbers generated by 50 run-throughs of my random walk. But when I use pylab.hist(batting_average, bins = 10), I get a weird multi-colored histogram that goes up close to 500, but with only 50 runs of the walk, the maximum it should be able to go on the y-axis would be 50.

Here’s my code:

a = ['Hit', 'Out']
b = [.3, .7]


def battingAverage(atBats, some_list=a, probabilities=b):
    num_hits = 0
    num_outs = 0
    current_BA = []
    for i in range(1,atBats):
        if random_pick(a, b) == 'Hit':
            num_hits += 1
        else:
            num_outs +=1
        BA = float(num_hits)/(float(num_hits)+float(num_outs))
        current_BA.append(BA)
    return current_BA

def printBAs():
    for i in range(50):
        batting_average = battingAverage(501)
        pylab.hist(batting_average, bins=10)

What’s wrong with my histogram!?

Let me know if anything needs clarification, and I’ll do my best.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:09:12+00:00Added an answer on May 26, 2026 at 4:09 am

    The argument passed to battingAverage is 501… and is the number of at-bats. You’re doing 50 histograms with 500 at-bats per histogram.

    (Oh, and you need to fix the formatting of your code… the indentation is messed up.)

    Your code doesn’t do what you think it does.

    I think you’re wanting battingAverage to return the final batting average, but it returns a list of batting averages, one for each at-bat.
    Then you’re plotting that list.

    I think you want to return a single number from battingAverage, and you want to accumulate the list in the printBAs() function, and move pylab.hist out of the for loop.

    I don’t suppose this is homework?

    In other words, I think you want something like this:

    a = ['Hit', 'Out']
    b = [.3, .7]
    
    def battingAverage(atBats, results=a, probabilities=b):
        num_hits = 0
        num_outs = 0
        for i in range(atBats):
            if random_pick(results, probabilities) == 'Hit':
                num_hits += 1
            else:
                num_outs +=1
        BA = float(num_hits)/(float(num_hits)+float(num_outs))
        return BA
    
    def printBAs():
        batting_averages = [battingAverage(500) for i in range(50)]
        pylab.hist(batting_averages, bins=10)
    

    Though that code still needs cleanup…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started working on Python and have been trying to run an outside
I'm working with python and am trying to extract numbers from a .txt file
HI! I'm trying to install opencv and use it with python, but when I
I am working in python on appengine. I am trying to create what is
I have a working Python based program that I want to run as a
Working in python 2.7. I'm sure the code is a little unwieldy, but I'll
This is my first time working with python. I'm trying to create a dictionary
I'm trying to get the argparse module working in Python. My problem is that
I have emacs and python working on OSX, but I'd like to get a
I'm returning to Java after a year spent working with Python, so I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.