这里以地震数据为例进行画图
首先需要先导入的库
import plotly.express as px
import plotly.io as pio
import pandas as pd
import numpy as np
#使用下面方法可以将生成的图以html格式保存
pio.write_html(fig, 'fig.html')
一、折线图
cntByYear = pd.read_csv('csv/countByYear.csv')
hover_labels = {'Year':'年份','count':'地震次数'}
fig3 = px.line(cntByYear, x='Year', y='count', title='2010-2021全球每年发生地震的次数'
,text='count', line_shape='spline',labels=hover_labels)
#可以通过设置line_shape参数来指定线条的形状。默认情况下,线条形状为'linear',即直线形状。
#除了'linear'之外,还可以设置为'spline'、'hvh'、'vhv'、'hv'和'vh'等形状。
fig3.update_traces(
textposition='top left')
fig3.update_traces(line=dict(color='#036579'))
# 显示折线图
fig3.show()
二、柱形图
cntByMonth = pd.read_csv('csv/countByMonth.csv')
hover_labels = {'Month':'月份','count':'地震次数','y':'平均每月发生量'}
cntByMonth["y"] = cntByMonth["count"] / 12
fig4 = px.bar(cntByMonth,
x = "Month",
y = "count",
title = '2010-2021全球每月发生地震的总次数',
labels=hover_labels,
color_continuous_scale = px.colors.sequential.Teal,
color="Month"
)
fig4.show()
默认横向,px.bar加入orientation='h'为纵向, opacity为透明度
三、饼图
1、圆形饼图
import pandas as pd
import plotly.graph_objects as go
cntByType = pd.read_csv('csv/countByType.csv')
colors = ['#559be3', '#89d2f2', '#9ddcc6', '#ffe268','#fb8c8a']
fig10 = go.Figure(data=[go.Pie(labels=cntByType['Type'],
values=cntByType['count'],
# hole=0.3, #控制环形中心空白大小
hoverinfo='label+percent' , #hoverinfo属性用于控制当用户将鼠标指针放到环形图上时,显示的内容
pull=[0.1,0.1,0,0],
marker=dict(colors=colors))])
fig10.update_layout(title='2010-2021年世界不同类型的地震占比')
fig10.show()
2、环形饼图
cntByType = pd.read_csv('csv/countByTypeC.csv')
colors = ['#559be3', '#89d2f2', '#9ddcc6', '#ffe268','#fb8c8a']
fig11 = go.Figure(data=[go.Pie(labels=cntByType['Type'],
values=cntByType['count'],
hole=0.7, #控制环形中心空白大小
hoverinfo='label+percent' , #hoverinfo属性用于控制当用户将鼠标指针放到环形图上时,显示的内容
pull=[0.1,0.1,0,0],
marker=dict(colors=colors))])
fig11.update_layout(title='2010-2021年中国地区不同类型的地震占比')
fig11.show()
pull可以调整饼图每项距圆心的距离
四、散点图
1、普通散点图
powAndDep = pd.read_csv('csv/powDeep.csv')
fig12 = px.scatter(powAndDep,
x = "Depth",
y = "Magnitude",
title = '震级与震源深度的关系图',
color = powAndDep.Depth,
color_continuous_scale = px.colors.sequential.Bluered,
opacity=0.45, # 透明度
)
fig12.show()
PS:渐变颜色由color_continuous_scale = px.colors.sequential.Bluered设定,具体颜色类别可参考PythonPlotly color_continuous_scale / Plotly 中内置命名连续(顺序、发散和循环)色标的参考。_plotly 颜色参数_33摄氏度的博客-CSDN博客
2、地图散点图
import plotly.express as px
import plotly.io as pio
import pandas as pd
import numpy as np
data = pd.read_csv('world.csv')
hover_labels = {'Date': '日期', 'Time': '时间', 'Longitude': '经度', 'Latitude': '纬度', 'Depth': '深度',
'Magtype': '编码类型', 'Magnitude': '震级', 'Area': '地区', 'Type': '类型', 'Remark': '标签'}
fig1 = px.scatter_geo(data,
color = data.Magnitude,
color_continuous_scale = px.colors.sequential.Inferno,
lon = data.Longitude,
lat = data.Latitude,
hover_name = data.Type,
hover_data = ["Area",
"Date",
"Time",
"Magnitude",
"Depth"
],
size = np.exp(data.Magnitude)/100,
labels=hover_labels,
projection ='natural earth',
title = '2010-2021年全球地震分布情况'
)
fig1.show()
带时间轴的地图散点图
data = pd.read_csv('csv/eq.csv')
hover_labels = {'Date': '日期', 'Latitude': '纬度', 'Longitude': '经度','Type': '类型', 'Depth': '深度', 'Magnitude': '震级',
'Time': '时间', 'Year': '年', 'Month': '月','Day': '日'}
fig2 = px.scatter_geo(data,
color = data.Magnitude,
color_continuous_scale = px.colors.sequential.Inferno,
lon = data.Longitude,
lat = data.Latitude,
animation_frame = data.Year,
hover_name = data.Type,
hover_data = ["Longitude",
"Latitude",
"Date",
"Time",
"Magnitude",
"Depth"
],
size = np.exp(data.Magnitude)/5,
labels=hover_labels,
projection = 'natural earth',
title = '1965-2016年全球地震分布随时间变化情况'
)
fig2.show()
五、地图热力图
p2_data = pd.read_csv("csv/earthquakeC.csv")
p2 = px.density_mapbox(p2_data, lat='Latitude', lon='Longitude', z='Magnitude', radius=5,
center=dict(lat=35, lon=108), zoom=3,
mapbox_style="stamen-terrain",
hover_data={'Latitude': False, 'Longitude': False, 'Magnitude': ':.2f'})
p2.show()
p2.write_html("p2.html", include_plotlyjs='cdn')