0

I've generated a lot of numbers using Python, saving them in a list, and now I want to plot them in a scatter graph and a bar graph to see the patterns of the function that I created.

I'm getting numbers from 0 to 99,999,999 at maximum, I tried to plot but I failed.

In the bar graph the y axis should show how many times a number repeated itself in the range of the numbers generated and the x axis the number itself.

I tried to use the method collections.counter but it keeps returning me a dict with all the numbers that appeard at least one time in the list, instead of the ones that just repeated, with the data of the numbers that repeated I think I could plot the graph properly.

Image showing the data that i get from the function

Image showing the data that i get from the function

5
  • 3
    One of your problems is that you store the numbers as strings instead of as numbers. Commented Jan 28, 2022 at 21:10
  • i also have a list with all of them, just int numbers, but i needed to use another function to get the number that each one repeats itself in a dictionary, i don't know if it is the most efficient way Commented Jan 28, 2022 at 21:19
  • For the dictionary, it is much better to create it using the integers directly as key, without converting them to strings. Commented Jan 28, 2022 at 22:59
  • 1
    I had a look at your data-in-a-picture, and well, except two numbers that have a count of two all the others appears only once in your list. I don't understand what you want to do, honest… Could you try to be more clear wrt what you want, especially with yourself? Commented Jan 29, 2022 at 9:30
  • basically i'm creating a function that will try to create random numbers, without using any library, then i'm storing them into a vector, i would like to have a graph that shows the numbers and if they repeat themselves but i can't figure how, i tried to use the collections.counter to get a dictionary with just the numbers that appears more than one time so i could plot in the graph Commented Jan 30, 2022 at 15:02

2 Answers 2

1

What would you like to plot in the scatter graph? Matplotlib has built-in histogram plotter.

import random
import matplotlib.pyplot as plt

random.seed(0)

ITER = 20
LOW = 0
HIGH = 10

RND_NUMS = []
for _ in range(ITER):
    RND_NUMS.append(random.randint(LOW, HIGH))

plt.hist(RND_NUMS, bins=range(0,10))

This produces something like:

histogram

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

Comments

0

you can use NumPy unique with return count set as true, here is an example with 10 random numbers,

import numpy as np
import matplotlib.pyplot as plt
# random integer array
rand_array=np.random.randint(10, size=10)    
u, c=np.unique(rand_array, return_counts=True)

# plot 
fig, axs=plt.subplots(1, 2)
axs[0].bar(range(len(c)), c)
axs[0].set_xticks(range(len(c)),u)
axs[0].set_yticks(range(max(c)))    
axs[0].set_title('unique counts')

# selecting out the repeated ones
axs[1].bar(range(np.sum(c>1)), c[c>1])
axs[1].set_xticks(range(np.sum(c>1)),u[c>1])
axs[1].set_title('repeated ones')
axs[1].set_yticks(range(max(c)))
plt.show()

the output result is sample code output

Comments

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.