Python疫情数据可视化分析+数据预测(pandas+pyecharts+statsmodels+matplotlib+sql)

1、MySQL数据库获取数据

此处的原始数据表是全国各省的实时数据集,现在只获取江苏省份的数据

engine = create_engine(mysq_conn) # 具体内容以自己上就可以为准
select_data = "select * from table_data where area='江苏'order by local_time desc"
df = pd.read_sql(sql=select_data, con=engine)
df.head()

Python疫情数据可视化分析+数据预测(pandas+pyecharts+statsmodels+matplotlib+sql)_第1张图片

2、转存数据集至本地csv,以免每次都要从数据库读取数据

3、日期字符串字段,数据清洗

#转化获取年-月-日
new_df['日期'] = new_df['local_time'].dt.year.map(str)+'-'+new_df['local_time'].dt.month.map(str)+'-'+new_df['local_time'].dt.day.map(str) 

Python疫情数据可视化分析+数据预测(pandas+pyecharts+statsmodels+matplotlib+sql)_第2张图片

4、数据可视化

from pyecharts import options as opts
from pyecharts.charts import Bar,Line,Pie,WordCloud,EffectScatter
from pyecharts.charts import PictorialBar
from pyecharts.globals import SymbolType
from pyecharts.globals import ThemeType
c = (
    Line(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
    .add_xaxis(x_label)
    .add_yaxis(
        "新增确诊",
        is_smooth=True, 
        symbol="emptyCircle", 
        is_symbol_show=False,
        color="#6e9ef1",
        y_axis=y_data,
        label_opts=opts.LabelOpts(is_show=False),
    ) 
    .set_series_opts(
        label_opts=opts.LabelOpts(
            is_show=False,
            position="inside",
            formatter="{c}",
        )
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="江苏省日新增确诊趋势图"),
        tooltip_opts=opts.TooltipOpts(is_show=True,axis_pointer_type= "cross",trigger="axis"),
        xaxis_opts=opts.AxisOpts(
            name_rotate=60,axislabel_opts={"rotate":45},
            axispointer_opts=opts.AxisPointerOpts(
                is_show=True,
            ),  
        ),
    )
)
    
c.render_notebook()

Python疫情数据可视化分析+数据预测(pandas+pyecharts+statsmodels+matplotlib+sql)_第3张图片

line=(
    Line()
    .add_xaxis(xaxis_data=x_label)
    .add_yaxis(
        series_name="最高新增城市",
        is_smooth=True,
        symbol="emptyCircle",
        is_symbol_show=False,
        color="#d14a61",
        y_axis=y_data,
    )
    .add_yaxis(
        series_name="全省新增",
        is_smooth=True,
        symbol="emptyCircle",
        is_symbol_show=False,
        color="#6e9ef1",
        y_axis=y_data2,
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="江苏省日新增确诊突然增原因"),
        legend_opts=opts.LegendOpts(),
        xaxis_opts=opts.AxisOpts(
            type_="category",
            axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
            axisline_opts=opts.AxisLineOpts(
                is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#d14a61")
            ),
            axispointer_opts=opts.AxisPointerOpts(
                is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
            ),
        ),
        yaxis_opts=opts.AxisOpts( 
            type_="value",
            splitline_opts=opts.SplitLineOpts(
                is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
            ),
        ),
    )
)
line.render_notebook()

Python疫情数据可视化分析+数据预测(pandas+pyecharts+statsmodels+matplotlib+sql)_第4张图片

def line_charts()->Line:
    line1 = (
        Line(init_opts=opts.InitOpts(width='1000px',height='500px'))        # 示例对象
        .add_xaxis(x_label)      # x轴数据
        .add_yaxis(
            "治愈率",  # 图例名称 
            is_smooth=True,  
            is_symbol_show=False,
            color="#6e9ef1",
            y_axis=y_label, # y轴数据
            label_opts=opts.LabelOpts(is_show=False),
            linestyle_opts=opts.LineStyleOpts(width=2), 
            yaxis_index=0,
        ) 
        # 全局配置:标题/副标题
        .set_global_opts(
            title_opts=opts.TitleOpts(title="全国治愈率/死亡率趋势图"),  # 标题
            xaxis_opts=opts.AxisOpts(
                name_rotate=60,axislabel_opts={"rotate":45},
            ),
            yaxis_opts=opts.AxisOpts(
                name='治愈率(%)',
                min_=70,
                max_=100,
                interval=10,  
                axislabel_opts=opts.LabelOpts(formatter='{value}%') 
            ),
            toolbox_opts=opts.ToolboxOpts( # 工具栏
                is_show = True,
                pos_top="top",
                pos_left="right",
                feature={  
                    "saveAsImage": {} ,
                    "dataZoom":{"yAxisIndex": "none"},
                    "restore": {} ,
                    "magicType":{"show": True, "type":["line","bar"]},
                    "dataView": {}
                },
            )
            
        )
        # bar 拓展
        .extend_axis(
             yaxis=opts.AxisOpts(             
                        name='死亡率(%)',
                        type_='value',  # 类型
                        min_=3,
                        max_=6,
                        interval=1,  
            )
        )
    )
    return line1


def line2_charts()->Line():
    line1=(
        Line()
        .add_xaxis(x_label)
        .add_yaxis(
            '死亡率',
            y_axis=y_label1,
            is_smooth=True, # 设置线为平滑的
            symbol="emptyCircle",  # 悬浮数据样式
            is_symbol_show=False,  # 显示数据
            color="#6e9ef1",
          )
    )
    return line1
    
line1=line_charts()
line2=line2_charts()
    
line1.overlap(line2).render_notebook() 

Python疫情数据可视化分析+数据预测(pandas+pyecharts+statsmodels+matplotlib+sql)_第5张图片

bar = (
    Line()
    .add_xaxis(xaxis_data=x_data)
    .add_yaxis(            
        series_name="累计确诊",
        y_axis=evaporation_capacity,
        yaxis_index=0,
        color=colors[1],
        label_opts=opts.LabelOpts(is_show=True),
        is_smooth=True, 
        symbol="emptyCircle",  
        is_symbol_show=False,  
        linestyle_opts=opts.LineStyleOpts(width=2),  
    )
    .add_yaxis(
        series_name="累计死亡",
        y_axis=rainfall_capacity,
        yaxis_index=1, 
        color=colors[0],
        label_opts=opts.LabelOpts(is_show=True),
        is_smooth=True, 
        symbol="emptyCircle", 
        is_symbol_show=False, 
        linestyle_opts=opts.LineStyleOpts(width=2),  
    )
    .extend_axis(
        yaxis=opts.AxisOpts(
            name="死亡",
            type_="value",
            min_=50000,
            max_=4000000,
            position="right",
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color=colors[1])
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value}"),
        )
    )
    .extend_axis(
        yaxis=opts.AxisOpts(
            
            type_="value",
            name="治愈",
            min_=50000,
            max_=150000000,
            position="left",
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color=colors[2])
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value}"),
            splitline_opts=opts.SplitLineOpts(
                is_show=True,
                linestyle_opts=opts.LineStyleOpts(opacity=1)
            ),
        )
    )
    .set_global_opts(
        yaxis_opts=opts.AxisOpts(
            type_="value",
            name="确诊",
            min_=50000,
            max_=170000000,
            position="right",
            offset=20,
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color=colors[0])
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value}"),
        ),
    )
)

line = (
    Line()
    .add_xaxis(xaxis_data=x_data)
    .add_yaxis(
        series_name="累计治愈",
        y_axis=average_temperature,
        color=colors[2],
        label_opts=opts.LabelOpts(is_show=True),
        is_smooth=True, 
    )
)

bar.overlap(line).render_notebook()

Python疫情数据可视化分析+数据预测(pandas+pyecharts+statsmodels+matplotlib+sql)_第6张图片

5、预测

Python疫情数据可视化分析+数据预测(pandas+pyecharts+statsmodels+matplotlib+sql)_第7张图片

你可能感兴趣的:(大数据可视化,python经验,pandas,大数据,python,数据分析,数据可视化,pyecahrts)