python数据可视化之plotly

1、安装

!pip install Plotly

2、导入包

import plotly.express as px
import plotly.graph_objects as go

3、使用

通过help查看函数参数说明

help(module_name.func_name) or __doc__ 方法看模块下特定函数的信息

3.1 绘制折线图

# 生成随机序列
df1 = pd.DataFrame(np.random.rand(30, 4))
# 绘制折线图
px.line(df1)
# 绘图另存为html
fig = px.bar(df1)
fig.write_html("bar.html")

3.2 绘制柱状图

# 图片另存为html

绘制地图

添加标记

## 初始化地图,指定北京市
m = folium.Map(
    location=[39.916485, 116.403119],
    zoom_start=14
)

# 添加marker到地图
folium.Marker([39.917248,116.397176], popup=parse_zhch('故宫博物院'), tooltip=tooltip).add_to(m)

# 使用Icon修改标记的颜色和样式
folium.Marker(
    location = [39.995209,116.394777], 
    popup=parse_zhch('北京市奥林匹克公园'), 
    tooltip=tooltip,
    icon=folium.Icon(color='green')
).add_to(m)
folium.Marker(
    location = [40.323042,116.223544], 
    popup=parse_zhch('北京市明十三陵景区'), 
    tooltip=tooltip,
    icon=folium.Icon(color='red')
).add_to(m)
folium.Marker(
    location = [39.881265,116.410638], 
    popup=parse_zhch('天坛公园'), 
    tooltip=tooltip,
    icon=folium.Icon(color='green' , prefix='fa' , icon='paw')
).add_to(m)

m

效果如下图:

python数据可视化之plotly_第1张图片

 区域标记

# folium区域标记
#radius半径单位为米,更多介绍参考:https://leafletjs.com/reference-1.6.0.html#circle
folium.Circle(
    location = [31.2453,121.4857], 
    radius = 100,
    popup=parse_zhch('外白渡桥'), 
    tooltip=tooltip,
    color='green',
    fill=False
).add_to(m)
folium.Circle(
    location = [31.2418,121.4953], 
    radius = 200,
    popup=parse_zhch('东方明珠'), 
    tooltip=tooltip,
    color='red',
    fill=True
).add_to(m)
m

效果如下图:

python数据可视化之plotly_第2张图片

当点比较多时可以使用plugins.MarkerCluster()聚合,缩小后显示数量,点击放大

m_beijing = folium.Map(
    location=[39.916485, 116.403119],
    zoom_start=11
)

limit = 1000
df_incident = df_incidents.iloc[0:limit, :]
#df_incident = df_incidents[df_incidents['cluster_id'].notnull()]

m_beijing.add_child(folium.LatLngPopup())

marker_cluster = plugins.MarkerCluster().add_to(m_beijing)

incidents = folium.map.FeatureGroup()

# for lat, lng in zip(df_incident.cluster_lat, df_incident.cluster_lng):
#     incidents.add_child(
#         folium.CircleMarker(
#         [lat, lng],
#         color='yellow',
#         fill=True,
#         fill_color='blue',
#         fill_capacity=0.6
#         )
#     )

# 添加聚合热力,类似贝壳找房
lat1 = list(df_incident.cluster_lat)
long1 = list(df_incident.cluster_lng)
labels = list(df_incident.city)
for lat, long, label in zip(lat1, long1, labels):
    folium.Marker([lat, long], popup=label).add_to(marker_cluster)
# add incidents to map
#m_beijing.add_child(incidents)
m_beijing

效果如下图: 

python数据可视化之plotly_第3张图片

行政区划

1、加载数据

geo = pd.read_csv("/root/tensorflow/data_analysis/base_data.csv")
geo.columns=["Regions","station_num"]
geo.head()

python数据可视化之plotly_第4张图片 

 2、加载行政围栏

with open("/root/tensorflow/data_analysis/plotly-choropleth-mapbox-demo-master/china_province.geojson") as f:
    provinces_map = json.load(f)

fig = go.Figure(
                go.Choroplethmapbox(
                                geojson=provinces_map,
                                featureidkey="properties.NL_NAME_1",
                                locations=geo.Regions,
                                z=geo.followerPercentage,
                                zauto=True,
                                colorscale='Cividis',
                                marker_opacity=0.5,
                                marker_line_width=0)
                )
fig.update_layout(mapbox_style="carto-darkmatter",
                 mapbox_zoom=3,
                 mapbox_center={"lat":35.8617,"lon":104.1954})
fig.update_layout(margin={"r":0, "t":0, "l":0, "b":0})
fig.show()

效果如下图(因为数据不全的缘故,部分省份未显示): 

python数据可视化之plotly_第5张图片

你可能感兴趣的:(可视化)