{"id":955,"date":"2021-07-14T09:00:31","date_gmt":"2021-07-14T01:00:31","guid":{"rendered":"https:\/\/104.es\/?p=955"},"modified":"2021-07-14T09:00:31","modified_gmt":"2021-07-14T01:00:31","slug":"django-plotly-basics","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2021\/07\/14\/django-plotly-basics\/","title":{"rendered":"\u5728Django\u7db2\u9801\u4e0a\u4f7f\u7528Plotly\u7e6a\u5716"},"content":{"rendered":"\n<p>\u4f7f\u7528Python\u8a9e\u8a00\u88fd\u4f5c\u5716\u8868\u6642\uff0c\u4e00\u822c\u90fd\u662f\u7fd2\u6163\u5229\u7528matplotlib\u7684\u5f37\u5927\u529f\u80fd\u3002\u4f46\u662f\uff0c\u5982\u679c\u60f3\u8981\u5728Django\u7684\u7db2\u9801\u4e0a\u88fd\u4f5c\u5716\u8868\u6642\uff0c\u6bd4\u8f03\u5bb9\u6613\u7684\u65b9\u5f0f\u5247\u662f\u4f7f\u7528Plotly\u9019\u500b\u6a21\u7d44\u3002\u8981\u4f7f\u7528Plotly\u9019\u500b\u6a21\u7d44\uff0c\u9996\u5148\u8981\u5229\u7528pip\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install plotly<\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c\u5728views.py\u4e2d\u7684\u7a0b\u5f0f\u78bc\uff0c\u8981\u7de8\u5beb\u6210\u5982\u4e0b\u6240\u793a\u7684\u6a23\u5b50\uff0c\u9019\u4e9b\u7a0b\u5f0f\u5c31\u662f\u5be6\u969b\u57f7\u884c\u7e6a\u5716\u7684\u90e8\u4efd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from plotly.offline import plot\nimport plotly.graph_objs as go\nimport numpy as np\n\ndef index(request):\n\tx = np.linspace(0, 2*np.pi, 360)\n\ty1 = np.sin(x)\n\ty2 = np.cos(x)\n\tplot_div = plot(&#091;go.Scatter(x=x, y=y1,\n\t\tmode='lines', name='SIN', text=\"Title\",\n\t\topacity=0.8, marker_color='green'),\n\t\tgo.Scatter(x=x, y=y2,\n\t\tmode='lines', name='COS', \n\t\topacity=0.8, marker_color='green')],\n\t\toutput_type='div')\n\n\treturn render(request, \"index.html\", locals())<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u9019\u500b\u7a0b\u5f0f\u4f8b\u4e2d\uff0c\u6211\u5011\u5229\u7528np.linspace\u7522\u751f\u4e86360\u500b0\u52302pi\u4e4b\u9593\u7684\u6578\u5b57\uff0c\u63a5\u8457\u5206\u5225\u8a08\u7b97\u51fa\u5b83\u5011\u5c0d\u61c9\u7684SIN\u548cCOS\u7684\u503c\uff0c\u653e\u5728y1\u548cy2\u4e2d\uff0c\u7136\u5f8c\u5c31\u9001\u9032Plotly\u4e2d\u7e6a\u5716\u3002\u5b8c\u6210\u4e4b\u5f8c\uff0c\u5728index.html\u4e2d\uff0c\u53ea\u8981\u52a0\u4e0a\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u78bc\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      {% autoescape off %}\n      {{ plot_div }}\n      {% endautoescape %}\n<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u5373\u70ba\u8f38\u51fa\u7684\u7d50\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1020\" height=\"417\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2021\/07\/image.png?w=1020\" alt=\"\" class=\"wp-image-960\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2021\/07\/image.png 1020w, https:\/\/104.es\/wp-content\/uploads\/2021\/07\/image-300x123.png 300w, https:\/\/104.es\/wp-content\/uploads\/2021\/07\/image-768x314.png 768w\" sizes=\"auto, (max-width: 1020px) 100vw, 1020px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Python\u8a9e\u8a00\u88fd\u4f5c\u5716\u8868\u6642\uff0c\u4e00\u822c\u90fd\u662f\u7fd2\u6163\u5229\u7528matplotlib\u7684\u5f37\u5927\u529f\u80fd\u3002\u4f46\u662f\uff0c\u5982\u679c\u60f3\u8981\u5728Django\u7684\u7db2\u9801\u4e0a\u88fd\u4f5c\u5716\u8868\u6642\uff0c\u6bd4\u8f03\u5bb9\u6613\u7684\u65b9\u5f0f\u5247\u662f\u4f7f\u7528Plotly\u9019\u500b\u6a21\u7d44\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-955","post","type-post","status-publish","format-standard","hentry","category-django"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/955","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=955"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/955\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}