{"id":2888,"date":"2023-08-06T18:22:48","date_gmt":"2023-08-06T10:22:48","guid":{"rendered":"https:\/\/104.es\/?p=2888"},"modified":"2023-08-06T18:22:49","modified_gmt":"2023-08-06T10:22:49","slug":"python-thingspeak-charts","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2023\/08\/06\/python-thingspeak-charts\/","title":{"rendered":"\u3010Python\u3011\u5373\u6642\u7e6a\u88fd\u4f86\u81ea\u65bcThingSpeak\u7684\u6578\u64da\u5716\u8868"},"content":{"rendered":"\n<p>\u4e4b\u524d\u6211\u5011\u628a\u8cc7\u6599\u5b9a\u6642\u4e0a\u50b3\u5230ThingSpeak\u7db2\u7ad9\u4e0a\u4e86\uff0c\u9019\u4e9b\u8cc7\u6599\u5c31\u53ef\u4ee5\u96a8\u6642\u4e0b\u8f09\u4e26\u52a0\u4ee5\u5206\u6790\u3002\u4e0d\u904e\u5728\u5206\u6790\u4e4b\u524d\uff0c\u9084\u662f\u8b93\u6211\u5011\u81ea\u5df1\u4f86\u756b\u756b\u770b\u8cc7\u6599\u7684\u7dda\u5716\u3002<\/p>\n\n\n\n<p>\u8981\u5728Python\u4e2d\u7e6a\u88fd\u5716\u8868\uff0c\u6700\u5e38\u7528\u7684\u5957\u4ef6\u662f matplotlib\uff0c\u5982\u679c\u8981\u9032\u884c\u8cc7\u6599\u5206\u6790\uff0c\u901a\u5e38\u90fd\u662f\u4f7f\u7528 pandas\uff0c\u8981\u4e0b\u8f09\u7db2\u9801\u7684\u8cc7\u6599\u7684\u8a71\uff0c\u6211\u5011\u4f7f\u7528\u7684\u662f requests\uff0c\u8655\u7406 JSON \u8cc7\u6599\u683c\u5f0f\uff0c\u7576\u7136\u662f\u7528 json\u3002<\/p>\n\n\n\n<p>\u904b\u7528ThingSpeak\u4e0a\u7684\u8cc7\u6599\u6709\u5169\u7a2e\u5e38\u898b\u7684\u65b9\u5f0f\uff0c\u7b2c\u4e00\u7a2e\u662f\u5230\u300c&nbsp;Import\/Export\u300d\u9801\u7c64\u4e2d\u53bb\u4e0b\u8f09\u6240\u6709\u7684\u8cc7\u6599\u56de\u4f86\uff0c\u5b83\u6703\u4ee5CSV\uff08\u4ee5\u9017\u865f\u5206\u9694\u7684\u8cc7\u6599\uff0c\u53ef\u4ee5\u4f7f\u7528Excel\u8b80\u53d6\uff09\u7684\u65b9\u5f0f\u5132\u5b58\u5230\u6211\u5011\u7684\u78c1\u789f\u4e2d\uff0c\u6211\u5011\u7684\u7a0b\u5f0f\u518d\u8f09\u5165\u6b64\u6a94\u6848\u52a0\u4ee5\u5206\u6790\u3002<\/p>\n\n\n\n<p>\u800c\u5176\u5be6\uff0c\u6bcf\u4e00\u500b\u516c\u958b\u7684Channel\u4e5f\u53ef\u4ee5\u900f\u904e\u4e0b\u8f09\u7684\u65b9\u5f0f\uff0c\u96a8\u6642\u7684\u53d6\u5f97\u6700\u8fd1\u7684100\u7b46\u8cc7\u6599\uff0c\u5982\u679c\u4f60\u8981\u7684\u8cc7\u6599\u53ea\u662f\u6700\u8fd1\u7684\u8cc7\u6599\uff0c\u90a3\u9ebc\u4f7f\u7528\u6b64\u7a2e\u65b9\u5f0f\u662f\u6700\u65b9\u4fbf\u7684\u3002<\/p>\n\n\n\n<p>\u8981\u5728\u7db2\u8def\u4e0a\u5373\u6642\u8b80\u53d6\u6700\u8fd1\u7684\u8cc7\u6599\uff0c\u9700\u8981\u5148\u8a2d\u5b9a\u70ba\u300cPublic View\u300d\uff0c\u5c31\u53ef\u4ee5\u7db2\u9801\u4e2d\u770b\u5230\u8981\u8b80\u53d6\u7684\u7db2\u5740\uff0c\u900f\u904e\u8a72\u7db2\u5740\u5c31\u53ef\u4ee5\u5728\u700f\u89bd\u5668\u770b\u5230\u8a72\u683c\u5f0f\u7684\u8cc7\u6599\uff0c\u4ee5JSON\u683c\u5f0f\u70ba\u4f8b\uff0c\u539f\u59cb\u8cc7\u6599\u5982\u4e0b\uff08\u7db2\u5740\uff1ahttps:\/\/thingspeak.com\/channels\/630561\/feed.json\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"982\" height=\"759\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-58.png\" alt=\"\" class=\"wp-image-2889\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-58.png 982w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-58-300x232.png 300w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-58-768x594.png 768w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-58-600x464.png 600w\" sizes=\"auto, (max-width: 982px) 100vw, 982px\" \/><\/figure>\n\n\n\n<p>\u6211\u5011\u7684\u505a\u6cd5\u662f\u900f\u904e\u8a72\u7db2\u5740\u628a\u9019\u7b46\u8cc7\u6599\u4e0b\u8f09\u5230\u8b8a\u6578\u4e2d\uff0c\u7136\u5f8c\u518d\u900f\u904ejson\u6a21\u7d44\u628a\u539f\u6709\u7684json\u683c\u5f0f\u7684\u8cc7\u6599\u8f49\u63db\u6210Python\u8a9e\u8a00\u4e2d\u7684\u5b57\u5178dict\u578b\u614b\uff0c\u63a5\u8457\u518d\u628a\u6211\u5011\u60f3\u8981\u7684\u5167\u5bb9\u653e\u5230Pandas\u7684DataFrame\u8cc7\u6599\u7d50\u69cb\u4e2d\u5c31\u53ef\u4ee5\u958b\u59cb\u6574\u7406\u8cc7\u6599\uff0c\u6216\u662f\u628a\u8cc7\u6599\u7e6a\u88fd\u6210\u5716\u8868\u4e86\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u7a0b\u5f0f\u78bc\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>%matplotlib inline\r\nimport pandas as pd\r\nimport requests\r\nimport json\r\n\r\nurl = 'https:\/\/thingspeak.com\/channels\/630561\/feed.json'\r\ndata = requests.get(url).text\r\njdata = json.loads(data)\r\nfeeds = jdata&#91;'feeds']\r\ndf = pd.DataFrame(feeds)\r\ntemperature = pd.to_numeric(df&#91;'field1'])\r\ntemperature.plot()\r\nhumidity = pd.to_numeric(df&#91;'field2'])\r\nhumidity.plot()<\/code><\/pre>\n\n\n\n<p>\u8acb\u7559\u610f\uff0c\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u78bc\u662f\u5728Jupyter notebook\u74b0\u5883\u4e2d\u57f7\u884c\u7684\u3002<\/p>\n\n\n\n<p>\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u78bc\u57f7\u884c\u7684\u7d50\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-59.png\" alt=\"\" class=\"wp-image-2890\" width=\"489\" height=\"259\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-59.png 557w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-59-300x159.png 300w\" sizes=\"auto, (max-width: 489px) 100vw, 489px\" \/><\/figure>\n<\/div>\n\n\n<p>\u5728\u9810\u8a2d\u7684\u60c5\u5f62\u4e0b\uff0c\u6bcf\u4e00\u6b21\u756b\u5716\u7684\u5167\u5bb9\u90fd\u6703\u88ab\u653e\u5728\u540c\u4e00\u5f35\u5716\u8868\u4e2d\uff0c\u5982\u679c\u4f60\u53ea\u662f\u8981\u770b\u5176\u4e2d\u4e00\u500b\u5716\u8868\uff08\u4f8b\u5982\u6eab\u5ea6\uff09\uff0c\u90a3\u9ebc\u53ea\u8981\u5148\u628a\u53e6\u4e00\u500b\u5716\u8868\u7684\u7e6a\u5716\u90e8\u4efd\u5148\u8a3b\u89e3\u8d77\u4f86\u5c31\u53ef\u4ee5\u4e86\uff0c\u7a0b\u5f0f\u78bc\u53ef\u4ee5\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>%matplotlib inline\r\nimport pandas as pd\r\nimport requests\r\nimport json\r\n\r\nurl = 'https:\/\/thingspeak.com\/channels\/630561\/feed.json'\r\ndata = requests.get(url).text\r\njdata = json.loads(data)\r\nfeeds = jdata&#91;'feeds']\r\ndf = pd.DataFrame(feeds)\r\ntemperature = pd.to_numeric(df&#91;'field1'])\r\ntemperature.plot()\r\n#humidity = pd.to_numeric(df&#91;'field2'])\r\n#humidity.plot()<\/code><\/pre>\n\n\n\n<p>\u6b64\u6642\u7684\u8f38\u51fa\u5716\u8868\u5c31\u6703\u53ea\u5448\u73fe\u6eab\u5ea6\u7684\u90e8\u4efd\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-60.png\" alt=\"\" class=\"wp-image-2891\" width=\"470\" height=\"239\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-60.png 556w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-60-300x153.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u6211\u5011\u628a\u8cc7\u6599\u5b9a\u6642\u4e0a\u50b3\u5230ThingSpeak\u7db2\u7ad9\u4e0a\u4e86\uff0c\u9019\u4e9b\u8cc7\u6599\u5c31\u53ef\u4ee5\u96a8\u6642\u4e0b\u8f09\u4e26\u52a0\u4ee5\u5206\u6790\u3002\u4e0d\u904e\u5728\u5206\u6790\u4e4b\u524d\uff0c\u9084\u662f\u8b93\u6211\u5011\u81ea\u5df1\u4f86\u756b\u756b\u770b\u8cc7\u6599\u7684\u7dda\u5716\u3002<\/p>\n","protected":false},"author":1,"featured_media":2891,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[15],"tags":[],"class_list":["post-2888","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"jetpack_featured_media_url":"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-60.png","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=2888"}],"version-history":[{"count":1,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2888\/revisions"}],"predecessor-version":[{"id":2892,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2888\/revisions\/2892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media\/2891"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}