#๐Ÿ”’ Help for creating normal distribution using matplotlib

15 messages ยท Page 1 of 1 (latest)

vale hound
#

Hello, my normal distribution curve does not look very accurate and I do not know if it is a problem with the variance calculation based on how the data is collected, or if it is an error using the matplotlib library

sly scarabBOT
#

@vale hound

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vale hound
#
import random
import matplotlib.pyplot as plt
import pandas as pd
import math
import scipy
from scipy.stats import norm
import numpy as np

binSize = 3
sampleSize = 5
numberOfTrials = 1000000

data = [
    122,
    114,
    94]

histogramList = []

maximum = max(data)
minimum = min(data)

maximum += binSize - (maximum - minimum) % binSize

lowRange = minimum
highRange = minimum + binSize - 1

meanList = []
for a in range(int((maximum-minimum)/(binSize-1))):
    histogramList.append([lowRange, highRange, 0])

    lowRange += binSize -1
    highRange += binSize-1

totalMean = 0
for b in range(numberOfTrials):
    sum = 0
    for i in range(sampleSize):
        value = data[random.randint(0,len(data)-1)]
        sum += value
    mean = sum / sampleSize
    totalMean += mean
    meanList.append(mean)

    for c in histogramList:
        lowRange, highRange, counter = c
        if mean >= lowRange and mean <= highRange:
            c[2] += 1
            break

xAxis = []
yAxis = []

variance = 0
finalMean = totalMean/numberOfTrials
for e in meanList:
    variance += (e-finalMean)**2

variance = variance/(numberOfTrials )
stdDev = math.sqrt(variance)

for d in histogramList:
    xAxis.append(f"{d[0]} - {d[1]}")
    yAxis.append(d[2])

df = pd.DataFrame({"Sample Mean": xAxis, 'Frequency': yAxis})

plt.bar(df['Sample Mean'], df['Frequency'], width=0.8)
plt.xticks(rotation = 90)
x = ((np.linspace(minimum, maximum, 100)))

pdf = norm.pdf(finalMean, finalMean, stdDev)
print(pdf)
save = 0
saveNumber = 0
for h in range(len(histogramList)-1):
    if histogramList[h][0] <= finalMean and histogramList[h][1] >= finalMean:
        saveNumber = h
        # save = (histogramList[h][0] + histogramList[h][1])/2
        save = histogramList[h][2]

factor = save / pdf
print(saveNumber, "save Number")
print(save, 'Save')
print(factor)

p = norm.pdf(x, finalMean, stdDev) * factor

plt.plot(x/(binSize)-(finalMean/binSize - saveNumber),p, 'k', linewidth = 2)
plt.show()

#

I get a curve that looks like this

#

but it definitely looks like it should be wider?

vale hound
#

How to change axis of matplotlib to regular axis

#

So the main problem that I have are the bins

#

do not actually act like integers

#

Like if I wanted my normal distribution to have a peak at x = 90

#

I would have to shift it because the axis of the matplot lib graph

#

where 90 is in the histogram is not actually 90

vale hound
#

!pastebin

sly scarabBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

sly scarabBOT
#

@vale hound

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.