id: 045bd164506544feac3aa0911d74d57d
parent_id: 
item_type: 1
item_id: d69c355e9dc84f0bb9b098181c241d4c
item_updated_time: 1764345627450
title_diff: "[{\"diffs\":[[1,\"【Python技术】akshare、matplotlib分析上证指数近10年月份涨跌情况\"]],\"start1\":0,\"start2\":0,\"length1\":0,\"length2\":44}]"
body_diff: "[{\"diffs\":[[1,\"<html>\\\r\\\n<head>\\\r\\\n  <title>【Python技术】akshare、matplotlib分析上证指数近10年月份涨跌情况</title>\\\r\\\n  <basefont face=\\\"微软雅黑\\\" size=\\\"2\\\" />\\\r\\\n  <meta http-equiv=\\\"Content-Type\\\" content=\\\"text/html;charset=utf-8\\\" />\\\r\\\n  <meta name=\\\"exporter-version\\\" content=\\\"YXBJ Windows/608924 (zh-CN, DDL); Windows/10.0.0 (Win64); EDAMVersion=V2;\\\"/>\\\r\\\n  <meta name=\\\"content-class\\\" content=\\\"yinxiang.peso.superNote\\\"/>\\\r\\\n  <meta name=\\\"application-data:yinxiang.noteLayout\\\" content=\\\"{\\\"lineSpace\\\":\\\"recommend\\\",\\\"paragraphSpace\\\":\\\"recommend\\\"}\\\"/>\\\r\\\n  <style>\\\r\\\n\\tbody, td {\\\r\\\n\\t  font-family: 微软雅黑;\\\r\\\n\\t  font-size: 12pt;\\\r\\\n\\t}\\\r\\\n  </style>\\\r\\\n</head>\\\r\\\n<body>\\\r\\\n<a name=\\\"39858\\\"></a>\\\r\\\n<h1>【Python技术】akshare、matplotlib分析上证指数近10年月份涨跌情况</h1>\\\r\\\n\\\r\\\n<div><span style=\\\"\\\"><div style=\\\"max-width: 700px;\\\"><p></p><div style=\\\"--en-blockquote: true; box-sizing: border-box; border-left: 3px solid #b4c0cc; background-position: initial initial; background-repeat: initial initial;\\\"><div>原文链接: <a href=\\\"http://mp.weixin.qq.com/s?__biz=MzI5NDAzNzExNw==&amp;mid=2247487144&amp;idx=1&amp;sn=fd759a9be48f60c8c57540d5cfeb8081&amp;chksm=ed6ba762bf18da66d7915be953aac1a670490bd311c7b112e724dac93d22c44adf78ae8e541e&amp;mpshare=1&amp;scene=1&amp;srcid=0414g7tjFCIiHxC4xcC88fxn&amp;sharer_shareinfo=e4e1dc8798cb07c2569ce3c43291508a&amp;sharer_shareinfo_first=e4e1dc8798cb07c2569ce3c43291508a#rd\\\"><span>http://mp.weixin.qq.com/s?__biz=MzI5NDAzNzExNw==&amp;mid=2247487...</span></a></div></div><p></p><div style=\\\"\\\"><div><div><div><span style=\\\"\\\">原创</span><span><span style=\\\"\\\">子晓0808</span></span><span><a style=\\\"\\\"> 子晓聊技术 </a></span><span><em></em></span></div><div style=\\\"\\\"><div style=\\\"text-align: center;\\\"><img src=\\\":/41c7a6716c444590a925b6c1f30902e4\\\" type=\\\"image/png\\\" data-filename=\\\"640.png\\\" style=\\\"--en-marks: %7B%22align%22%3A%22center%22%7D; width: auto;\\\" width=\\\"auto\\\"/></div><div><span>周末和朋友闲聊3月份的战绩，大家互相吐槽这行情的艰难。看了下群消息， 群里同学也在吐槽，怪量化么？ </span></div><div><span>对于自己来说，前面做得还算可以，下半月回撤了一些。后来我细想了下，还是怪自己太侥幸，自认为自己经过这几年的股市毒打，与之前已经不一样了，技术应该有了明显的进步。该休息的时候强行出手，非得证明下自己。不得不说， 1月2月赚钱只能说行情好，3月证明了自己的风险控制能力还是差了点。</span></div><div><span>为了验证下历年3月份的行情和接下来4月份的行情，最初我的想法 是拿出之前写的代码 运行3月份、4月份的结果看一看， 后来想了想， 每个月这样运行还是好麻烦，要不直接一步到位，直接看上证指数最近10年每个月的涨跌结果。</span></div><div><span>顶部截图就是大家想要的答案，好吧，看了下，历年3月、4月大盘大部分时候还是很惨的。</span></div><div><span><br/></span></div><div><span>相关文章推荐：</span></div><div><span><a href=\\\"https://mp.weixin.qq.com/s?__biz=MzI5NDAzNzExNw==&amp;mid=2247486765&amp;idx=1&amp;sn=bfe4ac55dbc3839caa1faec3f1fb9740&amp;scene=21#wechat_redirect\\\" style=\\\"\\\" target=\\\"_blank\\\">【Python技术】ashare、pyplot数据分析历年1月份A股表现</a></span></div><div><span><a href=\\\"https://mp.weixin.qq.com/s?__biz=MzI5NDAzNzExNw==&amp;mid=2247486733&amp;idx=1&amp;sn=16eb643814027bdf23973b0e0ba6e9f8&amp;scene=21#wechat_redirect\\\" style=\\\"\\\" target=\\\"_blank\\\">【Python技术】春节前后真的有红包行情么？用历史数据分析</a></span></div><div><span><br/></span></div><div><span><span style=\\\"font-weight: bold;\\\">题外话：</span></span></div><div><span>有些时候，我们不得不相信历史数据，毕竟历史数据还是有很强的参考意义。 对于做技术出身的我来说，还是很相信历史数据分析的。 </span></div><div><span>有些时候，只是为了证明 自己和 历史的自己 不一样 强行为之。 事实上，个人确实有了一些进步。  但在大趋势面前，这点小小的进步根本不值一提了。 </span></div><div><span>后续的目标，在提升收益率的同时， 在大盘情绪不佳的时候，减少出手频次。 该休息就休息。</span></div><div><span><br/></span></div><div><span>最后附上完整代码，需要的自取。 备注：</span><span style=\\\"color: rgba(0, 0, 0, 0.9); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.544px; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline;\\\"><span>如果发现格式有多余的特殊字符，用普通浏览器打开复制应该没问题。</span></span></div><div style=\\\"background-attachment: scroll; background-clip: border-box; background-color: rgba(0, 0, 0, 0); background-image: none; background-origin: padding-box; background-position-x: 0%; background-position-y: 0%; background-repeat: no-repeat; background-size: auto; width: auto; color: rgb(0, 0, 0); word-spacing: 0em; letter-spacing: 0em; word-break: break-word; overflow-wrap: break-word; text-align: left;\\\"><div style=\\\"border-radius: 5px; text-align: left;\\\"><div style=\\\"--en-codeblock: true; --en-meta: {&quot;title&quot;:&quot;&quot;,&quot;lang&quot;:&quot;Plain Text&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;showLine&quot;:true,&quot;lineWrap&quot;:true}; box-sizing: border-box; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;\\\"><pre><span>import streamlit as st</span><br/><span>import akshare as ak</span><br/><span>import pandas as pd</span><br/><span>import seaborn as sns</span><br/><span>import matplotlib.pyplot as plt</span><br/><span>import matplotlib.colors as mcolors</span><br/><span>from datetime import datetime</span><br/><span></span><br/><span># 设置全局样式</span><br/><span>plt.rcParams['font.'] = ['STHeiti']  # 苹果系统字体</span><br/><span># plt.rcParams['font.'] = ['SimHei']  # Windows系统使用此字体</span><br/><span>plt.rcParams['axes.unicode_minus'] = False</span><br/><span></span><br/><span># 配置页面</span><br/><span>st.set_page_config(page_title=&quot;上证指数历年月度分析&quot;, layout=&quot;wide&quot;)</span><br/><span>st.title(&quot;上证指数历年月度分析&quot;)</span><br/><span></span><br/><span># 自定义双色系</span><br/><span>dual_colors = ['#4CB050', '#FF3333']  # 绿跌红涨</span><br/><span>cmap = mcolors.ListedColormap(dual_colors)</span><br/><span>bounds = [-100, 0, 100]  # 严格分界</span><br/><span>norm = mcolors.BoundaryNorm(bounds, cmap.N)</span><br/><span></span><br/><span></span><br/><span># 数据获取</span><br/><span>@st.cache_data</span><br/><span>def get_data():</span><br/><span>    df = ak.stock_zh_index_daily(symbol=&quot;sh000001&quot;)</span><br/><span>    df['date'] = pd.to_datetime(df['date'])</span><br/><span>    df['year'] = df['date'].dt.year</span><br/><span>    df['month'] = df['date'].dt.month</span><br/><span>    return df[df['year'].between(2015, 2024)].sort_values('date')</span><br/><span></span><br/><span></span><br/><span># 构建月度数据</span><br/><span>def build_table(df):</span><br/><span>    monthly = []</span><br/><span>    for y in range(2015, 2025):</span><br/><span>        row = {'年份': y}</span><br/><span>        for m in range(1, 13):</span><br/><span>            sub = df[(df['year'] == y) &amp; (df['month'] == m)]</span><br/><span>            if len(sub) &gt;= 3:</span><br/><span>                chg = (sub.iloc[-1]['close'] / sub.iloc[0]['close'] - 1) * 100</span><br/><span>                row[f'{m}月'] = round(chg, 2)</span><br/><span>        monthly.append(row)</span><br/><span>    return pd.DataFrame(monthly).set_index('年份')</span><br/><span></span><br/><span></span><br/><span># 生成热力图</span><br/><span>def plot_heatmap(data):</span><br/><span>    fig, ax = plt.subplots(figsize=(16, 6))</span><br/><span>    sns.heatmap(data,</span><br/><span>                cmap=cmap,</span><br/><span>                norm=norm,</span><br/><span>                annot=True,</span><br/><span>                fmt=&quot;.1f&quot;,</span><br/><span>                linewidths=0.5,</span><br/><span>                cbar=False,</span><br/><span>                annot_kws={'color': 'white', 'weight': 'bold'})</span><br/><span>    ax.set_xticklabels(['1月', '2月', '3月', '4月', '5月', '6月',</span><br/><span>                        '7月', '8月', '9月', '10月', '11月', '12月'])</span><br/><span>    ax.set_title('月度涨跌幅分布', fontsize=14)</span><br/><span>    return fig</span><br/><span></span><br/><span></span><br/><span># 主程序</span><br/><span>df = get_data()</span><br/><span>monthly_df = build_table(df)</span><br/><span></span><br/><span># 显示表格</span><br/><span>st.subheader(&quot;数据表&quot;)</span><br/><span></span><br/><span></span><br/><span>def color_cell(val):</span><br/><span>    color = '#FF3333'if val &gt;= 0 else'#4CB050'</span><br/><span>    return f'background-color: {color}; color: white'</span><br/><span></span><br/><span></span><br/><span>styled_df = monthly_df.style.format('{:.1f}%', na_rep=&quot;-&quot;).applymap(color_cell)</span><br/><span>st.dataframe(styled_df, height=450, use_container_width=True)</span><br/><span></span><br/><span></span><br/></pre></div></div></div><div><span><br/></span></div><div><span><br/></span></div><div><span><span style=\\\"color: rgb(0, 82, 255);\\\">为了后续能正常收到文章推送，欢迎点赞在看，不然可能收不到消息哦。</span></span></div><div><span><br/></span></div></div></div></div></div></div></span>\\\n</div></body></html>\\u0000\"]],\"start1\":0,\"start2\":0,\"length1\":0,\"length2\":8608}]"
metadata_diff: {"new":{"id":"d69c355e9dc84f0bb9b098181c241d4c","parent_id":"f67288c3a0be4374b5d532b5dcc3aabc","latitude":"0.00000000","longitude":"0.00000000","altitude":"0.0000","author":"","source_url":"","is_todo":0,"todo_due":0,"todo_completed":0,"source":"joplin-desktop","source_application":"net.cozic.joplin-desktop","application_data":"","order":1764348829002,"markup_language":2,"is_shared":0,"share_id":"","conflict_original_id":"","master_key_id":"","user_data":"","deleted_time":0},"deleted":[]}
encryption_cipher_text: 
encryption_applied: 0
updated_time: 2025-11-28T21:04:04.081Z
created_time: 2025-11-28T21:04:04.081Z
type_: 13