python数据可视化实践

使用plotly实现动态交互图表可视化

https://blog.csdn.net/Xw_Classmate/article/details/123391056

五国疫情数据ploty

https://zhuanlan.zhihu.com/p/149025673

 

一个傻瓜式构建可视化 web的 Python 神器 -- streamlit

https://zhuanlan.zhihu.com/p/448853407

streamlit中文开发手册(详细版)

https://blog.csdn.net/weixin_44458771/article/details/135495928

 

①下面这个代码,运行一次再关掉,第二次就加载不出来了,是什么原因也没有细细看。

②报错也未处理,“A value is trying to be set on a copy of a slice from a DataFrame”有篇文章讲了这个错误。‘

③brazil和india的数据弄错了,导致显示不正常。

D:/pycharmProject/datanaly/test04.py:16: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df_interest.rename(

 

import plotly.express as px
import numpy as np
import pandas as pd



# url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv'
# req = requests.get(url, headers=headers, proxies=proxies, timeout=5)

df = pd.read_csv('time_series_covid19_deaths_global.csv')
print(df)
df_interest = df.loc[
    df['Country/Region'].isin(['United Kingdom', 'US', 'Italy', 'Brazil', 'India'])
    & df['Province/State'].isna()]
print(df_interest)
df_interest.rename(
    index=lambda x: df_interest.at[x, 'Country/Region'], inplace=True)
print(df_interest)
df1 = df_interest.transpose()
print(df1)
df1 = df1.drop(['Province/State', 'Country/Region', 'Lat', 'Long'])
df1 = df1.loc[(df1 != 0).any(axis=1)]
print(df1.index[:5])
df1.index = pd.to_datetime(df1.index, format='%m/%d/%y')
df1 = df1.diff() #数据每日变化

# fig = px.line(x=df1.index, y= df1[df1.columns[0]],title = 'Daily Deaths due to COVID-19', name = df1.columns[0])
# fig = px.line()
fig = px.line(x=df1.index, y=df1[df1.columns[0]], title='Daily Deaths due to COVID-19')

# add_scatter()属性。通过使用循环,我们可以添加所有范围内的国家。
for i,n in enumerate(df1.columns):
    fig.add_scatter(x=df1.index, y= df1[df1.columns[i]], name= df1.columns[i])

# 添加更多的细节,图中突出显示不同的数据点
fig.update_traces(mode='markers+lines')

# 添加相关的轴标签,设置字体大小并替换默认模板
# fig.update_layout(
#     title = 'Daily Deaths due to COVID-19'
#     ,xaxis_title = 'Dates'
#     ,yaxis_title = 'Number of Deaths'
#     ,font = dict(size = 25)
#     ,template = 'plotly_dark' #"plotly", "plotly_white", "plotly_dark", "ggplot2", "seaborn", "simple_white", "none"
# )

# 增加一个范围滑块
# fig.update_xaxes(rangeslider_visible=True)

fig.update_layout(
    updatemenus=[
        dict(
            type="buttons",
            direction="right",
            active=0,
            x=0.5,
            y=1.03,
            buttons=list([
                dict(label=df1.columns[0],
                     method="update",
                     args=[ {"visible": [True, False, False, False, False]},
                            {'showlegend' : True}
                        ]),
                dict(label=df1.columns[1],
                     method="update",
                     args=[ {"visible": [False, True, False, False, False]},
                            {'showlegend' : True}
                     ]),
                dict(label=df1.columns[2],
                     method="update",
                     args=[ {"visible": [False, False, True, False, False]},
                            {'showlegend' : True}
                        ]),
                dict(label=df1.columns[3],
                     method="update",
                     args=[ {"visible": [False, False, False, True, False]},
                            {'showlegend' : True}
                     ]),
                dict(label=df1.columns[4],
                     method="update",
                     args=[ {"visible": [False, False, False, False, True]},
                            {'showlegend' : True}
                           ]),
                dict(label='All',
                     method="update",
                     args=[ {"visible": [True, True, True, True, True]},
                            {'showlegend' : True}
                           ]),
            ]),
        )
    ]
)
fig.show()

 

posted on 2025-03-15 20:45  无名高地  阅读(19)  评论(0)    收藏  举报