#๐Ÿ”’ matplot histogram graph error

44 messages ยท Page 1 of 1 (latest)

west swallow
#

im getting an error here and im not sure whats wrong:

import numpy as np
from numpy import random
import matplotlib.pyplot as plt
from scipy.stats import norm, binom, poisson
# Binomial Distribution
n, p = 10, 0.5  # Number of trials and probability of success
data_binomial = random.binomial(n, p, 1000)

# Plot the histogram of the binomial distribution
plt.hist(data_binomial, bins=arange(0, n + 2) - 0.5, density=True, alpha=0.6, color='b')

# Add labels and title
plt.title('Binomial Distribution (n=10, p=0.5)')
plt.xlabel('Number of Successes')
plt.ylabel('Probability')

plt.show()

there traceback is:

    plt.hist(data_binomial, bins=arange(0, n + 2) - 0.5, density=True, alpha=0.6, color='b')
                                 ^^^^^^
NameError: name 'arange' is not defined. Did you mean: 'range'?```
fast monolithBOT
#

@west swallow

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.

gray gate
west swallow
#

ahhh

#

thats it

#

i forgot the np

#

thank u

#

how does that work though?

gray gate
#

"that" what exactly?

west swallow
#

thats making an array

#

the np.arange

gray gate
#

I'm not sure about what you mean

west swallow
#

np.arange(0, n+2)

#

is this making an array?

gray gate
#

do you understand what numpy is, what it does and what is its purpose?

west swallow
#

numpy is just a library right

gray gate
#

and what it does?

west swallow
#

arange is some function in the numpy library

#

i guess i dont?

gray gate
#

arange, not arrange

#

the main point of numpy is providing arrays that are more efficient than python lists and doing operations on them - it offers multiple methods to create arrays and do math operations on them

#

under the hood it is doing something like ```py
array = []
number = start
while number < end:
array.append(number)
number += step
return array

west swallow
#

whats the difference between an array and list?

gray gate
#

numpy arrays have a fixed shape, can only hold elements of one specific data type, and may have multiple dimensions

python lists can store any python object, you can change their length, and (although you can have nested lists), they have no real concept of dimensions

west swallow
#

when you say dimensions

#

what do u mean?

#

like x,y coordinate or something?

gray gate
#

[1, 2, 3] is a (3,) array, containing one dimensions of three elements

[[1, 2], [3, 4]] is a (2, 2) array, containing two dimensions, each of which has a length of 2

#

!e ```py
import numpy as np
array = np.array([
[10, 20],
[30, 40]
])

x = 0
y = 1

print(array[y, x])

fast monolithBOT
#

@gray gate :white_check_mark: Your 3.12 eval job has completed with return code 0.

30
west swallow
#

ah wait are the indexs in arrays like tied together somehow

#

so like the 1 and then 3 and the 2 and 4 are connected somehow where as in a nested list there is no such relationship

gray gate
#

!e not exactly```py
import numpy as np
array = np.array([
[10, 20],
[30, 40]
])

print(array)

take a row

print(array[0])
print(array[1])

take a column

print(array[:, 0])
print(array[:, 1])

fast monolithBOT
#

@gray gate :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | [[10 20]
002 |  [30 40]]
003 | [10 20]
004 | [30 40]
005 | [10 30]
006 | [20 40]
gray gate
#

I recommend reading the documentation, specially the user guides, if you haven't yet

west swallow
#

will do

#

as far as the bins= argument for the histogram though

#

how can that be an array?

#

shouldnt that just be an integer

west swallow
fast monolithBOT
#
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.