{"id":124,"date":"2023-11-25T15:24:00","date_gmt":"2023-11-25T15:24:00","guid":{"rendered":"https:\/\/doctor-dark.co.uk\/blog\/python-and-sql-with-matplotlib\/"},"modified":"2023-12-17T16:19:54","modified_gmt":"2023-12-17T16:19:54","slug":"python-and-sql-with-matplotlib","status":"publish","type":"post","link":"https:\/\/doctor-dark.co.uk\/blog\/python-and-sql-with-matplotlib\/","title":{"rendered":"Python and SQL with matplotlib."},"content":{"rendered":"<blockquote>\n<pre><span style=\"font-family: courier;\"># Quick hack to graph last 500 greenhouse temperatures from weather database.\n<\/span><span style=\"font-family: courier;\">import mariadb\n<\/span><span style=\"font-family: courier;\">import matplotlib.pyplot as plt<\/span>\n<span style=\"font-family: courier;\">conn = mariadb.connect(user=\"pi\",password=\"password\",host=\"localhost\",database=\"weather\")\n<\/span><span style=\"font-family: courier;\">cur = conn.cursor()<\/span>\n<span style=\"font-family: courier;\">tempIN\u00a0 \u00a0 \u00a0= []\n<\/span><span style=\"font-family: courier;\">tempOUT\u00a0 \u00a0 = []\n<\/span><span style=\"font-family: courier;\">timestamps = []<\/span>\n<span style=\"font-family: courier;\"># Get the most recent 500 records.\n<\/span><span style=\"font-family: courier;\">cur.execute(\"SELECT greenhouse_temperature, ambient_temperature, created FROM WEATHER_MEASUREMENT \n<\/span><span style=\"font-family: courier;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0ORDER BY created DESC LIMIT 500\")<\/span>\n<span style=\"font-family: courier;\">for i in cur:\n<\/span><span style=\"font-family: courier;\">\u00a0 \u00a0 tempIN.append(i[0])\n<\/span><span style=\"font-family: courier;\">\u00a0 \u00a0 tempOUT.append(i[1])\n<\/span><span style=\"font-family: courier;\">\u00a0 \u00a0 timestamps.append(i[2])\u00a0\u00a0\n<\/span><span style=\"font-family: courier;\">conn.close()<\/span>\n<span style=\"font-family: courier;\">plt.figure(figsize=(14, 6))\n<\/span><span style=\"font-family: courier;\">plt.title(label=\"Greenhouse and outside temperature up to \"+str(timestamps[0]))\n<\/span><span style=\"font-family: courier;\">plt.xlabel(\"Date and time\")\n<\/span><span style=\"font-family: courier;\">plt.ylabel(\"Temperature in Celsius\")\n<\/span><span style=\"font-family: courier;\">plt.plot(timestamps, tempIN, label='Greenhouse temperature')\n<\/span><span style=\"font-family: courier;\">plt.plot(timestamps, tempOUT, label='Outside temperature')\n<\/span><span style=\"font-family: courier;\">plt.axhline(y=5.0, color='r', linestyle='-.')\n<\/span><span style=\"font-family: courier;\">plt.legend()\n<\/span><span style=\"font-family: courier;\">plt.savefig(\"\/var\/www\/html\/GHtemp.jpg\")\n<\/span><span style=\"font-family: courier;\">plt.show()<\/span><\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p># Quick hack to graph last 500 greenhouse temperatures from weather database. import mariadb import matplotlib.pyplot as plt conn = mariadb.connect(user=&#8221;pi&#8221;,password=&#8221;password&#8221;,host=&#8221;localhost&#8221;,database=&#8221;weather&#8221;) cur = conn.cursor() tempIN\u00a0 \u00a0 \u00a0= [] tempOUT\u00a0 \u00a0 = [] timestamps = [] # Get the most recent 500 records. cur.execute(&#8220;SELECT greenhouse_temperature, ambient_temperature, created FROM WEATHER_MEASUREMENT \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0ORDER &hellip; <a href=\"https:\/\/doctor-dark.co.uk\/blog\/python-and-sql-with-matplotlib\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python and SQL with matplotlib.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-124","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":3,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":410,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/124\/revisions\/410"}],"wp:attachment":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}