吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 830|回复: 23
收起左侧

[学习记录] 行业板块主力资金净占比分布热力图

[复制链接]
psqladm 发表于 2025-3-30 22:41
本帖最后由 苏紫方璇 于 2025-3-31 00:02 编辑

当日行业板块主力资金净占比分布热力图, 实时更新(5秒),拿走不谢,不喜勿喷。
[Python] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
#!/usr/bin/env python
# coding=utf-8
 
from __future__ import (absolute_import, division, print_function, unicode_literals)
 
import time
 
import akshare as ak
import pandas as pd
import plotly.express as px
from dash import Dash, html, dcc, Input, Output
 
app = Dash(__name__)
 
 
def create_figure():
    # 获取实时数据
    try:
        df = ak.stock_sector_fund_flow_rank(
            indicator="今日",
            sector_type="行业资金流"
        )
    except Exception as e:
        print(f"数据获取失败:{e}")
        return
 
    # 数据预处理
    df.columns = df.columns.str.replace("今日", '', regex=False)
    df = df.rename(columns={'名称': '板块名称'})
    df['资金净流入(亿)'] = round(df['主力净流入-净额'] / 100000000, 2)
    df = df.sort_values('主力净流入-净占比', ascending=False).dropna()
 
    # 动态计算显示比例
    min_negative = df['主力净流入-净占比'].min()
    df['size_weight'] = df['主力净流入-净占比'].apply(
        lambda x: (1 / abs(min_negative) * (x - min_negative) + 1)
    )
 
    # 生成图表
    fig = px.treemap(
        df,
        path=['板块名称'],
        values='size_weight',
        color='主力净流入-净占比',
        color_continuous_scale='RdYlGn_r',
        color_continuous_midpoint=0,
        range_color=[df['主力净流入-净占比'].min(), df['主力净流入-净占比'].max()],
        height=800,
        width=1600,
        title='🎨行业板块主力资金净占比分布热力图 (📌数据更新于: {})'.format(pd.Timestamp.now().strftime("%H:%M:%S")),
        branchvalues='total',
        hover_data={
            '涨跌幅': ':.2f%',
            '资金净流入(亿)': ':.2f',
            '主力净流入-净占比': ':.2f%'
        }
    )
 
    # 样式配置
    fig.update_traces(
        texttemplate=(
            "<b>%{label}</b><br>"
            "&#128200;%{customdata[0]:.2f}%<br>"
            "&#128176;%{customdata[1]:.2f}亿"
        ),
        hovertemplate=(
            "<b>%{label}</b><br>"
            "&#128200;涨跌幅: %{customdata[0]:.2f}%<br>"
            "&#128176;资金净流入: <b>%{customdata[1]:.2f}</b>亿<br>"
            "&#9878;&#65039;主力净占比: %{customdata[2]:.2f}%"
        ),
        textfont=dict(size=12, color='black', family="SimHei")
    )
 
    return fig
 
 
app.layout = html.Div([
    dcc.Graph(id='live-graph'),
    dcc.Interval(
        id='interval-component',
        interval=5 * 1000# 5秒间隔
        n_intervals=0
    )
])
 
 
@app.callback(
    Output('live-graph', 'figure'),
    Input('interval-component', 'n_intervals')
)
def update_graph(n):
    try:
        return create_figure()
    except Exception as e:
        print(f"更新失败: {str(e)}")
        return px.scatter(title="&#9888;&#65039;数据加载失败,请检查网络连接")
 
 
if __name__ == "__main__":
    app.run_server(debug=True)

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
jiukou + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

苏紫方璇 发表于 2025-3-31 00:01
代码插入可以参考置顶帖
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/thread-713042-1-1.html
(出处: 吾爱破解论坛)
 楼主| psqladm 发表于 2025-3-31 05:25
本帖最后由 psqladm 于 2025-3-31 05:50 编辑
苏紫方璇 发表于 2025-3-31 00:01
代码插入可以参考置顶帖
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/threa ...

谢谢,学习了。
屏幕截图 2025-03-31 054951.png
jiemax 发表于 2025-3-31 08:02
jiukou 发表于 2025-3-31 08:26
虽然不会弄,但看着也很不错,感谢分享!
luxiaoqi0811 发表于 2025-3-31 08:27
有没有大神帮忙转一下的,不会用啊~
ROSE1688 发表于 2025-3-31 08:38
有没有成品?
HA19683 发表于 2025-3-31 08:40
感谢分享开源
wjhobbby 发表于 2025-3-31 08:47
还不会用,感谢分享,收藏先
eniaclyl 发表于 2025-3-31 08:47
效果还不错
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-7 15:47

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表