HISTOGRAM WITH DATA FROM A SQLITE3 TABLE
Listing 5.18 displays the content of rainfall_hist2.py that shows you how to define a simple SQL query to create a histogram based on the data from the rainfall table.
LISTING 5.18: rainfall_hist3.py
import sqlite3
import pandas as pd
import matplotlib.pyplot as plt
sql = """
SELECT
cast(centimeters/5.00 as int)*5 as cent_floor,
count(*) as count
FROM rainfall
GROUP by 1
ORDER by 1;
"""
con = sqlite3.connect("mytools.db")
df = pd.read_sql_query(sql, con)
con.close()
print("=> Histogram of Rainfall:")
print(df)
#df.hist(column='count', bins=7, grid=False, rwidth=1.0, color='red')
df.hist(column='count', bins=14, grid=False, rwidth=.8, color='red')
plt.show()
Listing 5.18 starts with several import statements and then initializes the variable sql with a SQL statement that selects data from the rainfall table. The next portion of Listing 5.18...