For an assignment in class, I made a number generator that writes the output into a file. I need to read this file and make a histogram showing how many values are in specific ranges like 0-9, 10-19, 20-29, etc. The problem I'm running into is that I can't change the x-values on the bottom of the graph. I want it to be the same as 0-9, 10-19, etc but I can't find anything online that'll let me change it to that.
This is my code
import os
from matplotlib import pyplot as plt
os.chdir("/Users/elian/Desktop/School/Scripting/Week 7")
with open("Random.txt", "r") as file:
values = file.read()
contents = values.split()
mapped_contents = map(int, contents)
contents = list(mapped_contents)
file.close()
# histogram = plt.hist(contents, bins=binset, )
plt.hist(contents, bins=10)
plt.ylabel("Amount")
plt.xlabel("Range")
plt.title("Random Number Generator Histogram")
plt.show()
This is my first time working with matplotlib so I apologize if I'm not explaining everything right. I just want the histogram to show the amount of numbers that are in a specific range. I've tried range
and xlim
but I still run into the problem where the x-axis increments in 20s.
CodePudding user response:
The command for placing values on the axis is plt.xticks(ticks, labels)
. However, in case of a histogram, the values are automatically arranged as per the data and bin size. So if you change the bin size, you would be able to see the values accordingly.
CodePudding user response:
You can specify the x values in the bins parameter itself by passing a list of the values.
Example: plt.hist(data, bins=[1, 2, 3, 4, 5]) The histogram would look as given in the link below.