Created
June 2, 2024 19:36
-
-
Save AllanRPereira/010a4bfc05b9416f961491447c80ecf8 to your computer and use it in GitHub Desktop.
Create Histogram F159
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| def frequencia(eventos): | |
| freq_dados = [] | |
| for dist, number in eventos: | |
| dist = float(dist) | |
| for _ in range(int(number)): | |
| freq_dados.append(dist) | |
| return freq_dados | |
| dados = """41,9 1 | |
| 42 0 | |
| 42,1 1 | |
| 42,2 2 | |
| 42,3 0 | |
| 42,4 1 | |
| 42,5 5 | |
| 42,6 2 | |
| 42,7 3 | |
| 42,8 5 | |
| 42,9 11 | |
| 43 4 | |
| 43,1 7 | |
| 43,2 9 | |
| 43,3 13 | |
| 43,4 4 | |
| 43,5 13 | |
| 43,6 6 | |
| 43,7 8 | |
| 43,8 3 | |
| 43,9 0 | |
| 44 0 | |
| 44,1 1 | |
| 44,2 0 | |
| 44,3 1""" | |
| dados = dados.replace(",", ".") | |
| eventos = [info.split("\t") for info in dados.split("\n")] | |
| freq_dados = frequencia(eventos) | |
| n = len(freq_dados) | |
| min_x = min(freq_dados) | |
| max_x = max(freq_dados) | |
| media = sum(freq_dados) / n | |
| desvio = (sum([(medida - media)**2 for medida in freq_dados]) / (n - 1))**(1/2) | |
| delta_barra = (max_x - min_x)/round(n ** (1/2)) | |
| for i in range(20): | |
| if max_x >= (min_x + i * delta_barra) and (min_x + (i + 1) * delta_barra) > max_x: | |
| n_barra = i + 1 | |
| break | |
| media = round(media, 1) | |
| desvio = round(desvio, 1) | |
| texto = f"""Média: {media} | |
| Desvio padrão: {desvio} | |
| ∆x: {round(delta_barra, 1)} cm | |
| Número de pontos: {n}""" | |
| tipo_esfera = input("Digite o tipo de esfera:") | |
| plt.xlabel("Distância (cm)") | |
| plt.ylabel("Contagem") | |
| plt.text(42, 24, texto) | |
| plt.title(f"Histograma - Lançamento esfera de {tipo_esfera}") | |
| count, bins, ignored = plt.hist(freq_dados, bins=n_barra, range=(min_x, min_x + n_barra * delta_barra), rwidth=1) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment