Python Hub

How do I plot a graph in Python from MySQL database tables using matplotlib ????

3      Reply

I found an example that works. Try this

import matplotlib.pyplot as plt
import pandas
import MySQLdb

# connect to MySQL database 
conn = MySQLdb.connect(host="localhost", user="xx", passwd="xxxx", db="dbname")


# this is the query we will be making 
query = """ 
SELECT TimeStamp, Pac  
FROM SolarData  
WHERE TimeStamp >= "2014-01-03"
  AND TimeStamp < "2014-01-04"; 
"""

df = pandas.read_sql(query, conn, index_col=['TimeStamp'])
fig, ax = plt.subplots()
df.plot(ax=ax)
conn.close()
Reply

Thanks so much.

Happy to help :)

Super friendly <br/>code & developer communities

Super friendly
code & developer communities

A place for coders and developers to share, learn and grow together.