{"id":986594,"date":"2024-12-27T07:46:17","date_gmt":"2024-12-26T23:46:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/986594.html"},"modified":"2024-12-27T07:46:19","modified_gmt":"2024-12-26T23:46:19","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0k%e7%ba%bf%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/986594.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0k\u7ebf\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063110\/c1b71917-f5fd-4227-ad9b-f2a52d8ad191.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0k\u7ebf\u56fe\" \/><\/p>\n<p><p> <strong>\u5b9e\u73b0Python K\u7ebf\u56fe\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Matplotlib\u3001Plotly\u548cmplfinance\u7b49\u5e93\u3002\u8fd9\u91cc\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528Matplotlib\u548cmplfinance\u8fd9\u4e24\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2dmplfinance\u662f\u4e13\u4e3a\u91d1\u878d\u6570\u636e\u53ef\u89c6\u5316\u8bbe\u8ba1\u7684\u5e93\uff0c\u529f\u80fd\u66f4\u4e3a\u5f3a\u5927\u4e14\u4f7f\u7528\u7b80\u4fbf\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u5b9e\u73b0K\u7ebf\u56fe<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u9759\u6001\u3001\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u56fe\u5f62\u3002\u901a\u8fc7Matplotlib\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u57fa\u672c\u7684K\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u548c\u5bfc\u5165Matplotlib\u5e93\u4ee5\u53caPandas\u5e93\u7528\u4e8e\u5904\u7406\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<p>from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY<\/p>\n<p>from matplotlib.dates import date2num<\/p>\n<p>import matplotlib.ticker as ticker<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u51c6\u5907\u6570\u636e<\/h3>\n<\/p>\n<p><p>K\u7ebf\u56fe\u901a\u5e38\u9700\u8981\u5f00\u76d8\u4ef7\uff08Open\uff09\u3001\u6700\u9ad8\u4ef7\uff08High\uff09\u3001\u6700\u4f4e\u4ef7\uff08Low\uff09\u548c\u6536\u76d8\u4ef7\uff08Close\uff09\u6570\u636e\u3002\u53ef\u4ee5\u4ece\u91d1\u878d\u6570\u636e\u63d0\u4f9b\u5546\u5982Yahoo Finance\u3001Alpha Vantage\u7b49\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u96c6<\/p>\n<p>data = {<\/p>\n<p>    &#39;Date&#39;: [&#39;2023-01-01&#39;, &#39;2023-01-02&#39;, &#39;2023-01-03&#39;, &#39;2023-01-04&#39;, &#39;2023-01-05&#39;],<\/p>\n<p>    &#39;Open&#39;: [100, 102, 104, 103, 105],<\/p>\n<p>    &#39;High&#39;: [105, 106, 108, 107, 110],<\/p>\n<p>    &#39;Low&#39;: [99, 101, 102, 101, 104],<\/p>\n<p>    &#39;Close&#39;: [104, 103, 107, 106, 109]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&#39;Date&#39;] = pd.to_datetime(df[&#39;Date&#39;])<\/p>\n<p>df[&#39;Date_Num&#39;] = df[&#39;Date&#39;].apply(lambda date: date2num(date))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u7ed8\u5236K\u7ebf\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u521b\u5efaK\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p>ax.xaxis.set_major_locator(WeekdayLocator(MONDAY))<\/p>\n<p>ax.xaxis.set_minor_locator(DayLocator())<\/p>\n<p>ax.xaxis.set_major_formatter(DateFormatter(&#39;%b %d&#39;))<\/p>\n<p>def plot_candlestick(ax, data):<\/p>\n<p>    for idx, row in data.iterrows():<\/p>\n<p>        if row[&#39;Close&#39;] &gt;= row[&#39;Open&#39;]:<\/p>\n<p>            color = &#39;green&#39;<\/p>\n<p>            lower = row[&#39;Open&#39;]<\/p>\n<p>            height = row[&#39;Close&#39;] - row[&#39;Open&#39;]<\/p>\n<p>        else:<\/p>\n<p>            color = &#39;red&#39;<\/p>\n<p>            lower = row[&#39;Close&#39;]<\/p>\n<p>            height = row[&#39;Open&#39;] - row[&#39;Close&#39;]<\/p>\n<p>        ax.add_patch(plt.Rectangle((row[&#39;Date_Num&#39;] - 0.3, lower), 0.6, height, color=color))<\/p>\n<p>        ax.plot([row[&#39;Date_Num&#39;], row[&#39;Date_Num&#39;]], [row[&#39;Low&#39;], row[&#39;High&#39;]], color=&#39;black&#39;)<\/p>\n<p>plot_candlestick(ax, df)<\/p>\n<p>ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, _: pd.to_datetime(num2date(x)).strftime(&#39;%Y-%m-%d&#39;)))<\/p>\n<p>fig.autofmt_xdate()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u5206\u6790\u548c\u6539\u8fdb<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7ed8\u5236K\u7ebf\u56fe\u53ef\u4ee5\u63d0\u4f9b\u6211\u4eec\u6240\u9700\u7684\u57fa\u672c\u529f\u80fd\uff0c\u4f46\u5b83\u9700\u8981\u624b\u52a8\u5904\u7406\u8bb8\u591a\u7ec6\u8282\uff0c\u5982\u65e5\u671f\u683c\u5f0f\u3001\u989c\u8272\u8bbe\u7f6e\u7b49\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u548c\u4ea4\u4e92\u6027\u66f4\u5f3a\u7684\u9700\u6c42\uff0cmplfinance\u53ef\u80fd\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001MPLFINANCE\u5b9e\u73b0K\u7ebf\u56fe<\/p>\n<\/p>\n<p><p>mplfinance\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u91d1\u878d\u6570\u636e\u53ef\u89c6\u5316\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u4e14\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u5bfc\u5165mplfinance<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mplfinance<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import mplfinance as mpf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u51c6\u5907\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e0eMatplotlib\u76f8\u540c\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907OHLC\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>3. \u7ed8\u5236K\u7ebf\u56fe<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528mplfinance\u7ed8\u5236K\u7ebf\u56fe\u76f8\u5bf9\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mpf.plot(df.set_index(&#39;Date&#39;), type=&#39;candle&#39;, style=&#39;charles&#39;, volume=True, title=&#39;K-line Chart&#39;, ylabel=&#39;Price ($)&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u81ea\u5b9a\u4e49K\u7ebf\u56fe<\/h3>\n<\/p>\n<p><p>mplfinance\u5141\u8bb8\u5bf9\u56fe\u8868\u8fdb\u884c\u5e7f\u6cdb\u7684\u81ea\u5b9a\u4e49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mpf.plot(df.set_index(&#39;Date&#39;), type=&#39;candle&#39;, style=&#39;charles&#39;, volume=True,<\/p>\n<p>         title=&#39;K-line Chart&#39;, ylabel=&#39;Price ($)&#39;, figratio=(16,9), figscale=1.2,<\/p>\n<p>         mav=(3,6,9), show_nontrading=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5. \u5206\u6790\u548c\u6539\u8fdb<\/h3>\n<\/p>\n<p><p>mplfinance\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5982\u79fb\u52a8\u5e73\u5747\u7ebf\u3001\u4ea4\u6613\u91cf\u7b49\u6307\u6807\u7684\u96c6\u6210\uff0c\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u5206\u6790\u5e02\u573a\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p><strong>Matplotlib\u548cmplfinance\u90fd\u662f\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5728Python\u4e2d\u5b9e\u73b0K\u7ebf\u56fe\u3002Matplotlib\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\u548c\u5e7f\u6cdb\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u800cmplfinance\u5219\u4e13\u6ce8\u4e8e\u91d1\u878d\u6570\u636e\u7684\u53ef\u89c6\u5316\uff0c\u63d0\u4f9b\u4e86\u7b80\u5316\u7684\u63a5\u53e3\u548c\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u6839\u636e\u60a8\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u5b9e\u73b0\u6570\u636e\u7684\u53ef\u89c6\u5316\u548c\u5206\u6790\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236K\u7ebf\u56fe\uff1f<\/strong><br \/>\u8981\u7ed8\u5236K\u7ebf\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2aPython\u5e93\uff0c\u5982Matplotlib\u3001Plotly\u548cmplfinance\u7b49\u3002mplfinance\u662f\u4e13\u95e8\u7528\u4e8e\u7ed8\u5236\u91d1\u878d\u56fe\u8868\u7684\u5e93\uff0c\u4f7f\u7528\u8d77\u6765\u7b80\u5355\u4e14\u529f\u80fd\u5f3a\u5927\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\uff0c\u5e76\u5bfc\u5165\u76f8\u5e94\u7684\u6a21\u5757\u3002\u63a5\u7740\uff0c\u51c6\u5907\u597d\u5305\u542b\u65f6\u95f4\u3001\u5f00\u76d8\u4ef7\u3001\u6700\u9ad8\u4ef7\u3001\u6700\u4f4e\u4ef7\u548c\u6536\u76d8\u4ef7\u7684\u5386\u53f2\u6570\u636e\uff0c\u4f7f\u7528mplfinance\u4e2d\u7684<code>plot()<\/code>\u51fd\u6570\u5373\u53ef\u8f7b\u677e\u7ed8\u5236K\u7ebf\u56fe\u3002<\/p>\n<p><strong>\u7ed8\u5236K\u7ebf\u56fe\u9700\u8981\u54ea\u4e9b\u6570\u636e\uff1f<\/strong><br \/>\u7ed8\u5236K\u7ebf\u56fe\u6240\u9700\u7684\u6570\u636e\u5305\u62ec\u65f6\u95f4\u5e8f\u5217\uff08\u65e5\u671f\u6216\u65f6\u95f4\uff09\u3001\u5f00\u76d8\u4ef7\u3001\u6700\u9ad8\u4ef7\u3001\u6700\u4f4e\u4ef7\u548c\u6536\u76d8\u4ef7\u3002\u8fd9\u4e9b\u6570\u636e\u901a\u5e38\u4ee5\u8868\u683c\u5f62\u5f0f\u5448\u73b0\uff0c\u7528\u6237\u53ef\u4ee5\u4eceCSV\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216API\u83b7\u53d6\u3002\u786e\u4fdd\u6570\u636e\u7684\u65f6\u95f4\u987a\u5e8f\u6b63\u786e\uff0c\u4ee5\u4fbf\u751f\u6210\u51c6\u786e\u7684\u56fe\u8868\u3002<\/p>\n<p><strong>Python\u7ed8\u5236K\u7ebf\u56fe\u65f6\u5982\u4f55\u5904\u7406\u7f3a\u5931\u6570\u636e\uff1f<\/strong><br \/>\u5728\u5904\u7406K\u7ebf\u56fe\u65f6\uff0c\u7f3a\u5931\u6570\u636e\u53ef\u80fd\u5bfc\u81f4\u56fe\u8868\u4e0d\u51c6\u786e\u3002\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5904\u7406\u7f3a\u5931\u6570\u636e\uff0c\u4f8b\u5982\u4f7f\u7528\u524d\u4e00\u4e2a\u6709\u6548\u503c\u586b\u5145\u7f3a\u5931\u503c\uff0c\u6216\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u3002\u53e6\u4e00\u79cd\u9009\u62e9\u662f\u5229\u7528\u63d2\u503c\u65b9\u6cd5<a href=\"https:\/\/docs.pingcode.com\/agile\/project-management\/estimation\" target=\"_blank\">\u4f30\u7b97<\/a>\u7f3a\u5931\u503c\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\uff0c\u4ee5\u4fbf\u751f\u6210\u6709\u6548\u7684K\u7ebf\u56fe\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b9e\u73b0Python K\u7ebf\u56fe\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Matplotlib\u3001Plotly\u548cmplfinance\u7b49\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":986598,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986594"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=986594"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986594\/revisions"}],"predecessor-version":[{"id":986599,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986594\/revisions\/986599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/986598"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=986594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=986594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=986594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}