So I’m trying to create a histogram from the data in my created sqlite database.
I extract the data in the form of a histogram type table:
cnx = sqlite3.connect('practice.db')
#pd.read_sql_query
sql = pd.read_sql_query('''
SELECT CAST((deliverydistance/1)as int)*1 as bin, count(*)
FROM orders
group by 1
order by 1;
''',cnx)
The output looks something like this in terminal:
Now when I extract using python, I use the following code to put the values in an array:
distance = []
counts = []
for x,y in sql.iterrows():
y = y["count(*)"]
counts.append(y)
distance.append(x)
print(distance)
print(counts)
Resulting in this:
distance = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
count = [57136, 4711, 6569, 7268, 6755, 5757, 7643, 6175, 7954, 9418, 4945, 4178, 2844, 2104, 1829, 9, 4, 1, 3]
Now when I plot the histogram, I would like count to be on the y axis and the distance to be on the x axis.
plt.hist(counts, bins = distance)
I get the following graph:
How would I get my the ‘counts’ on the y axis and distance on the x axis?