So I want to create a program that will display a graph depending on the number placed in by the user, it will ask for the number of days, then depending on the number of days, it will ask the user to input the sales for each day that runs with loop. Then it will collect all the data for every day, then displays them on a graph when the days are all done. My current code is messily slapped together from parts based off of other code, and when I try to run it, it will display "No module named 'matplotlib.pylot'"
This is my current code
''' def chart_maker():
import matplotlib.pylot as plt
#Asks user for the number of days
days = input(int("Please enter the number of days greater or equal to 2"))
# asks user to input the sales for each day.
saleslist=[]
i = 1
for i in range(len(days)):
saledays = input(float("Please enter the sales for day" i ))
saleslist.append(salesdays)
#Title
ax.set_title('Sales per Day')
#X-axis
ax.set_xlabel('Day')
#Y-axis
ax.set_ylabel('Sales')
#Displays the bar graph.
fig, ax = plt.subplots()
n, bins, patches = ax.hist(x, num_bins)
fig, ax = plt.subplots()
plt.bar([days], saleslist)
chart_maker()
'''
CodePudding user response:
I have updated the code and corrected errors to make the function that will do what you require. The indentation in python is very important, so do copy it as is, including indentations. Variable name corrections and logic changes are few other items that were required.
def chart_maker():
import matplotlib.pyplot as plt
#Asks user for the number of days
days = int(input("Please enter the number of days greater or equal to 2 : "))
if days < 2 : #Exit if user enters number less that 2
print("Entered number less than 2. Exiting...")
return
# asks user to input the sales for each day.
saleslist=[]
i = 1
for i in range(1,days 1):
saledays = float(input("Please enter the sales for day {}: ".format(i)))
saleslist.append(saledays)
#Displays the bar graph.
fig, ax = plt.subplots()
plt.bar(x=range(1, days 1), height=saleslist)
#Title
ax.set_title('Sales per Day')
#X-axis
ax.set_xlabel('Day')
ax.set(xticks=np.arange(1, days 1)) #Show tick marks in X-axis at 1, 2,....
#Y-axis
ax.set_ylabel('Sales')
chart_maker() #Outside function - the call to chart_maker
Output
Please enter the number of days greater or equal to 2 : 4
Please enter the sales for day 1: 3
Please enter the sales for day 2: 6
Please enter the sales for day 3: 4
Please enter the sales for day 4: 8