%pip install matplotlib pandas numpy seaborn plotly --upgrade8 可视化
8.1 Matplotlib数据可视化
Matplotlib模块是Python最基础的可视化库,可以绘制多种形式的图形,包括线图、直方图、饼状图、散点图、误差线图等等。在我们的工作和学习中,将数据图形化展示是一项非常重要的任务,可以让数据更加直观。
Matplotlib 最重要的特性之一就是具有良好的操作系统兼容性和图形显示底层接口兼容性。
import matplotlib.pyplot as plt
from matplotlib import animation
import pandas as pd
import numpy as np
import matht = np.linspace(0, math.pi, 1000)
x = np.sin(t)
y = np.cos(t) + np.power(x, 2.0/3)
plt.plot(x, y, color='red', linewidth=2, label='h')
plt.plot(-x, y, color='red', linewidth=2, label='-h')
plt.xlabel('t')
plt.ylabel('h')
plt.ylim(-2, 2)
plt.xlim(-2, 2)
# plt.show()
设置绘图样式
使用 plt.style 来选择图形的绘图风格。
从 Matplotlib 3.6 版本开始,seaborn 样式已被移除。可以使用以下替代方案:
seaborn-v0_8- 使用旧版 seaborn 样式default- Matplotlib 默认样式ggplot- ggplot2 风格bmh- Bayesian Methods for Hackers 样式
# 查看所有可用样式
print(plt.style.available)['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'petroff10', 'seaborn-v0_8', 'seaborn-v0_8-bright', 'seaborn-v0_8-colorblind', 'seaborn-v0_8-dark', 'seaborn-v0_8-dark-palette', 'seaborn-v0_8-darkgrid', 'seaborn-v0_8-deep', 'seaborn-v0_8-muted', 'seaborn-v0_8-notebook', 'seaborn-v0_8-paper', 'seaborn-v0_8-pastel', 'seaborn-v0_8-poster', 'seaborn-v0_8-talk', 'seaborn-v0_8-ticks', 'seaborn-v0_8-white', 'seaborn-v0_8-whitegrid', 'tableau-colorblind10']
# 使用 seaborn-v0_8 样式(兼容旧代码)
plt.style.use('seaborn-v0_8')关于plt.show()
如果你在一个脚本文件中使用 Matplotlib,那么显示图形的时候必须使用 plt.show(),而在notebook中可以不要,特别注意在notebook中画图:
%matplotlib notebook会在 Notebook 中启动交互式图形。%matplotlib inline会在 Notebook 中启动静态图形。
import numpy as np
x = np.linspace(0, 10, 100)
fig = plt.figure()
plt.plot(x, np.sin(x), '-')
plt.plot(x, np.cos(x), '--')
fig.savefig('my_figure.png')from IPython.display import Image
Image('my_figure.png')
fig.savefig('my_figure.pdf')fig.canvas.get_supported_filetypes(){'eps': 'Encapsulated Postscript',
'jpg': 'Joint Photographic Experts Group',
'jpeg': 'Joint Photographic Experts Group',
'pdf': 'Portable Document Format',
'pgf': 'PGF code for LaTeX',
'png': 'Portable Network Graphics',
'ps': 'Postscript',
'raw': 'Raw RGBA bitmap',
'rgba': 'Raw RGBA bitmap',
'svg': 'Scalable Vector Graphics',
'svgz': 'Scalable Vector Graphics',
'tif': 'Tagged Image File Format',
'tiff': 'Tagged Image File Format',
'webp': 'WebP Image Format'}
plt.figure(figsize= (8,6)) # 创建图形
# 创建两个子图中的第一个,设置坐标轴
plt.subplot(2, 1, 1) # (行、列、子图编号)
plt.plot(x, np.sin(x))
# 创建两个子图中的第二个,设置坐标轴
plt.subplot(2, 1, 2)
plt.plot(x, np.cos(x));
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x))
ax.plot(x, np.cos(x))
plt.plot(x, x + 0, linestyle='solid')
plt.plot(x, x + 1, linestyle='dashed')
plt.plot(x, x + 2, linestyle='dashdot')
plt.plot(x, x + 3, linestyle='dotted');
# 你可以用下面的简写形式
plt.plot(x, x + 4, linestyle='-') # 实线
plt.plot(x, x + 5, linestyle='--') # 虚线
plt.plot(x, x + 6, linestyle='-.') # 点划线
plt.plot(x, x + 7, linestyle=':'); # 实点线
import numpy as np
import matplotlib.pyplot as plt
data = np.random.randn(1000)
plt.hist(data)(array([ 9., 26., 93., 174., 257., 249., 122., 46., 16., 8.]),
array([-3.18364243, -2.51917038, -1.85469833, -1.19022628, -0.52575423,
0.13871782, 0.80318987, 1.46766192, 2.13213397, 2.79660602,
3.46107807]),
<BarContainer object of 10 artists>)

8.2 Pandas plot
df = pd.DataFrame(np.random.randn(1000, 4),
index=pd.date_range('1/1/2000', periods=1000),
columns=list('ABCD'))
df = df.cumsum()
df.head()| A | B | C | D | |
|---|---|---|---|---|
| 2000-01-01 | -1.148129 | 0.919846 | 0.233806 | 0.540728 |
| 2000-01-02 | -0.567908 | 0.484401 | -0.363631 | 0.858367 |
| 2000-01-03 | -1.167174 | 1.308936 | -1.344971 | 0.673636 |
| 2000-01-04 | -1.150034 | -0.318474 | -0.568011 | 0.752190 |
| 2000-01-05 | 0.322542 | -0.712590 | -0.737587 | 0.828602 |
df.A.hist()
df["gp"] = df.A // 15df.gp.drop_duplicates()2000-01-01 -1.0
2000-01-05 0.0
2000-07-02 1.0
2000-08-10 2.0
Name: gp, dtype: float64
df.head()| A | B | C | D | gp | |
|---|---|---|---|---|---|
| 2000-01-01 | -1.148129 | 0.919846 | 0.233806 | 0.540728 | -1.0 |
| 2000-01-02 | -0.567908 | 0.484401 | -0.363631 | 0.858367 | -1.0 |
| 2000-01-03 | -1.167174 | 1.308936 | -1.344971 | 0.673636 | -1.0 |
| 2000-01-04 | -1.150034 | -0.318474 | -0.568011 | 0.752190 | -1.0 |
| 2000-01-05 | 0.322542 | -0.712590 | -0.737587 | 0.828602 | 0.0 |
df.A.hist(by= df.gp, figsize=(10, 6))array([[<Axes: title={'center': '-1.0'}>,
<Axes: title={'center': '0.0'}>],
[<Axes: title={'center': '1.0'}>, <Axes: title={'center': '2.0'}>]],
dtype=object)

df.A.plot(kind='box')
df.plot(kind='box')
df.plot(figsize= (10,5))
df = abs(df)del df["gp"]df.plot.area()
df.iloc[5].plot(kind='bar', figsize= (5,5))
df.plot.scatter(x = "A", y = "B", figsize= (6,6))
df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df2.plot.bar()
df2.plot.bar(stacked=True)
df2.plot.barh(stacked=True)
df2 = df2.div(df2.sum("columns"), axis = "rows")df2.plot.barh(stacked=True)
df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df.head()| a | b | c | d | |
|---|---|---|---|---|
| 0 | 0.063116 | 0.455439 | 0.068626 | 0.991551 |
| 1 | 0.086392 | 0.590133 | 0.494357 | 0.592609 |
| 2 | 0.747400 | 0.149594 | 0.604976 | 0.344975 |
| 3 | 0.803134 | 0.331363 | 0.168731 | 0.512835 |
| 4 | 0.249216 | 0.822752 | 0.270448 | 0.135452 |
df.plot.scatter(x='a', y='b')
df.plot.scatter(x='a', y='b', c='c', s=100)
df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])
df['b'] = df['b'] + np.arange(1000)
df.plot.scatter(x='a', y='b')
df.plot.hexbin(x='a', y='b', gridsize=25)
import seaborn as snsimport seaborn as sns
sns.jointplot(x="a", y="b", data=df, kind="kde")
8.3 Plotly 交互式可视化
cufflinks 库已停止维护,且与新版本的 plotly/numpy 存在兼容性问题。
推荐使用 Plotly Express,它是现代化的、官方支持的交互式可视化库。
安装 Plotly
%pip install plotly kaleido --upgradePlotly Express 基础示例
Plotly Express 提供了简洁的 API 来创建交互式图表。
import plotly.express as px
import pandas as pd
import numpy as np
# 创建示例数据
df = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100),
'category': np.random.choice(['A', 'B', 'C'], 100)
})
df.head()| x | y | category | |
|---|---|---|---|
| 0 | 1.912363 | 0.058237 | C |
| 1 | 1.061946 | 1.374703 | A |
| 2 | -0.846091 | 0.394709 | A |
| 3 | 0.351086 | -0.668664 | C |
| 4 | 0.453259 | -0.699143 | C |
# 散点图
fig = px.scatter(df, x='x', y='y', color='category',
title='交互式散点图',
labels={'x': 'X轴', 'y': 'Y轴'})
fig.show()# 创建时间序列数据
ts_df = pd.DataFrame({
'date': pd.date_range('2024-01-01', periods=100),
'value': np.cumsum(np.random.randn(100))
})
# 线图
fig = px.line(ts_df, x='date', y='value',
title='时间序列图',
labels={'date': '日期', 'value': '数值'})
fig.show()# 直方图
fig = px.histogram(df, x='x', color='category',
title='分组直方图',
nbins=20)
fig.show()# 箱线图
fig = px.box(df, x='category', y='y',
title='箱线图',
labels={'category': '类别', 'y': '数值'})
fig.show()- 交互式:可以缩放、平移、悬停查看数据
- 现代化:API 简洁,文档完善
- 导出方便:支持导出为 HTML、PNG、PDF 等格式
- 样式丰富:内置多种主题和配色方案
8.4 Seaborn 可视化库
https://seaborn.pydata.org/index.html
Seaborn是基于matplotlib的图形可视化python包。它提供了一种高度交互式界面,便于用户能够做出各种有吸引力的统计图表。
https://seaborn.pydata.org/examples/index.html
# library
import seaborn as sns
import pandas as pd
import numpy as np
# Create a dataset (fake)
df = pd.DataFrame(np.random.random((5,5)), columns=["a","b","c","d","e"])
# Default heatmap: just a visualization of this square matrix
p1 = sns.heatmap(df)
# library
import seaborn as sns
import pandas as pd
import numpy as np
# Create a dataset (fake)
df = pd.DataFrame(np.random.random((100,5)), columns=["a","b","c","d","e"])
# Calculate correlation between each pair of variable
corr_matrix=df.corr()
# plot it
sns.heatmap(corr_matrix, cmap='PuOr')
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")
# Load the example diamonds dataset
diamonds = sns.load_dataset("diamonds")
# Draw a scatter plot while assigning point colors and sizes to different
# variables in the dataset
f, ax = plt.subplots(figsize=(6.5, 6.5))
sns.despine(f, left=True, bottom=True)
clarity_ranking = ["I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF"]
sns.scatterplot(x="carat", y="price",
hue="clarity", size="depth",
palette="ch:r=-.2,d=.3_r",
hue_order=clarity_ranking,
sizes=(1, 8), linewidth=0,
data=diamonds, ax=ax).get_figure().savefig("output.png", dpi=600)
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="darkgrid")
# Load an example dataset with long-form data
fmri = sns.load_dataset("fmri")
# Plot the responses for different events and regions
sns.lineplot(x="timepoint", y="signal",
hue="region", style="event",
data=fmri).get_figure()
plt.savefig('saving-a-seaborn-plot-as-png-file-transparent.png',
transparent=True, dpi = 600)
8.5 folium 地图可视化
https://python-visualization.github.io/folium/
%pip install folium --upgradeimport folium
local = [23.132, 113.345]
# local = [23.155, 113.327]
# local = [23.019, 113.411]
m = folium.Map(location=local,zoom_start=16)
mlocal = [23.087, 113.37]
# 使用 OpenStreetMap 默认底图(推荐)
m = folium.Map(
location=local,
zoom_start=12
)
folium.Circle(
radius=800,
location=[23.133, 113.342],
popup='暨南大学石牌校区',
color='crimson',
fill=False,
).add_to(m)
folium.CircleMarker(
location=[23.155, 113.327],
radius=15,
popup='暨南大学华文校区',
color='#3186cc',
fill=True,
fill_color='#3186cc'
).add_to(m)
folium.CircleMarker(
location=[23.02829562968323, 113.41585435512872],
radius=20,
popup='暨南大学番禺校区',
color='#3186cc',
fill=True,
fill_color='#3186cc'
).add_to(m)
m# generated data
import numpy as np
data = (
np.random.normal(size=(300, 3)) *
np.array([[0.05, 0.05, 0.05]]) +
np.array([[23.087, 113.3, 1]])
).tolist()
data[[23.101546158896962, 113.3559836955326, 0.9894410386499295],
[23.206066012475347, 113.28795855682371, 0.9697259884862655],
[23.08392476288006, 113.31453781250104, 1.1457594502764785],
[23.05744528889505, 113.26004875931372, 0.9663768346529729],
[23.15062299486233, 113.27805517048259, 0.9699878594343987],
[23.053550813569373, 113.32890588328601, 1.0285583972598435],
[23.11700101151718, 113.2645146717489, 1.0123019799199646],
[22.988245823027835, 113.36205459061783, 1.0213025595499607],
[23.06797485277542, 113.2475510613123, 1.0678908634916637],
[23.113647836037643, 113.26745103577356, 0.9833261378031858],
[23.02788304183722, 113.29248277873555, 1.0485264172966904],
[23.086198525528356, 113.31983241241606, 0.9861298841373867],
[23.03678805466783, 113.2178792720758, 1.0300796832941865],
[23.03190415207701, 113.31423851297714, 0.9875371525376696],
[23.133864486563954, 113.33765968641369, 0.954020298362893],
[23.05061989931659, 113.26879211682939, 1.0318210550935296],
[23.080373304865525, 113.32076671521173, 0.9484319637298709],
[23.06565902220455, 113.24147309164248, 1.0191142810226768],
[23.09457688506146, 113.20814558271736, 1.0587279469062612],
[23.033043142349737, 113.25947713610235, 0.971192305711924],
[23.133188934447073, 113.20943452320223, 0.985551648762346],
[23.061611564875616, 113.21007840055954, 0.9545026360980701],
[22.92949340772551, 113.3917423787921, 0.9671716890919827],
[23.10480392649642, 113.31245600651629, 0.9504886581013213],
[23.04687459649217, 113.31398187808027, 0.9538575967131679],
[23.084922768638346, 113.32529193747344, 0.9873995724465672],
[23.08896541891964, 113.35785372525008, 0.9157596898432434],
[23.106167720661947, 113.35278371290791, 1.0355012834131738],
[23.05893028657467, 113.2929090241397, 0.9934086301799155],
[23.100254528276455, 113.28569365438582, 0.9697582497976975],
[23.100267991995757, 113.286648687338, 1.0056439386208098],
[23.03259614630495, 113.24719218805389, 1.0839653809248087],
[22.97580135199514, 113.24816441660852, 1.0551549850624784],
[23.103506845381656, 113.22330985200823, 0.9569660339952333],
[23.08155740623373, 113.31613205616186, 1.0326594920042422],
[23.073514276242808, 113.32540798577915, 0.9606159335638937],
[23.103565120240706, 113.29486106671403, 0.9517507480906046],
[23.130036247698392, 113.30598496230941, 1.0315628910852292],
[23.06172600519158, 113.34370785449661, 0.8643292849416976],
[23.08350620732617, 113.3118986748248, 0.9598038207035481],
[23.102995587263003, 113.2895455264216, 0.9318667962924058],
[23.10204806419112, 113.27763963289941, 1.0139441449767197],
[23.115735263735523, 113.27383146677859, 1.0348766682524373],
[23.132802851003458, 113.32455084150797, 0.9994405837394807],
[23.049495981452957, 113.30419036994795, 1.0122565627558844],
[23.147660250287952, 113.25536669971527, 1.0755398448963323],
[23.094723730180903, 113.31657460963422, 0.9582743372937494],
[23.062005972898888, 113.25758907214419, 0.9749254976806571],
[22.991771318835386, 113.2812616857264, 0.9328772228469442],
[23.049192713120952, 113.34566886812418, 1.1096961100540148],
[23.179543669195223, 113.30634395230997, 1.0076373939597834],
[23.159172272036777, 113.38710064786338, 1.0377810437776769],
[23.094354898266655, 113.36640501532717, 1.0164539529046976],
[23.09323607284136, 113.24652490903779, 1.0128609793741368],
[23.094476930990293, 113.32342885554453, 1.096327285304553],
[23.105007514905655, 113.30529102790933, 0.9890556942384832],
[23.091909321239278, 113.32022865360726, 0.9251134127624585],
[23.090656888748512, 113.2022509894404, 1.0047565942782015],
[23.10606563497312, 113.23501090279797, 1.0389226317344133],
[23.185574921177714, 113.31506088060226, 1.015511034342598],
[23.102342136665346, 113.30388305894634, 0.9119640157887134],
[23.09668312329003, 113.2582863727005, 0.9852901900639525],
[23.116175383856547, 113.3496341901885, 1.0915732474658644],
[23.07708212017613, 113.37017657700088, 1.0225520330642228],
[23.086635919287826, 113.25674682791953, 1.000633665169261],
[23.06894690904764, 113.32549528878612, 1.0469078046666602],
[23.154272229223178, 113.21089497578609, 1.0263571839012067],
[23.073836627667013, 113.34570114123244, 1.035732742264827],
[23.07420299554474, 113.30710595491108, 0.9879650610930449],
[23.11858561257243, 113.32592757308089, 0.9450176998938496],
[23.088559030678386, 113.26314798633604, 0.9849678240197471],
[23.092829938297204, 113.3655845037067, 1.0367888707997495],
[23.03049972223146, 113.26097158613538, 1.0245420831333782],
[23.082442911994377, 113.42196232903193, 0.9851451567888506],
[23.02411571633941, 113.32108872844569, 0.9510298949858036],
[23.08991603247511, 113.2987189027804, 0.9240252083410196],
[23.042445220352434, 113.19280546817197, 0.9949373451178886],
[23.142057078532737, 113.27334082216913, 1.135848859000205],
[23.044166853422606, 113.24246022491229, 0.9810834442319656],
[23.103931467669263, 113.31492372045238, 0.9278816522198023],
[23.103881652639437, 113.26037895188306, 1.0545027097131334],
[23.031695645572015, 113.248340515816, 0.9969239310787659],
[23.03082136012085, 113.29253051857123, 0.9989543654854337],
[23.055958700010013, 113.30430725570966, 1.0957409015343604],
[23.089140984979867, 113.33144474615686, 1.0266459650104982],
[23.118319616195976, 113.26298829712078, 0.9689674177943207],
[23.077475738629357, 113.28911213474665, 1.0172518729918973],
[22.952284938177062, 113.33681720264026, 0.9170150081491624],
[23.200036503806867, 113.30214750970147, 1.0603565271169324],
[23.003771493334824, 113.30415844415325, 0.9512732654977779],
[23.103204831569524, 113.35727060713299, 1.0444823453666077],
[23.097919089350608, 113.20240969019638, 0.9619512464096537],
[23.10843029829919, 113.38770649554057, 1.0082074407738013],
[23.175133216252163, 113.33260435280593, 0.9943749116946058],
[23.167183751728953, 113.26602378851273, 0.9219507863435309],
[23.08743018875501, 113.25406109007916, 1.0151754103621693],
[23.136870550173224, 113.32119096703018, 1.003565775868763],
[23.12793182141984, 113.26215981628003, 0.9962469445693592],
[23.10231837640217, 113.29553830872608, 0.9155928482775786],
[23.175745276009383, 113.21698140066228, 0.9898214801815608],
[23.1085401870954, 113.22508174832579, 1.0398980968170766],
[23.066480181254942, 113.34927654908746, 1.0202787086027927],
[23.044712979840856, 113.27304058943267, 1.0595017791206822],
[23.065222357960188, 113.36301273166636, 1.0857198710386575],
[23.064704484873037, 113.19498840267134, 1.0146185288968683],
[23.08884366737496, 113.33693441670778, 1.0036040256761976],
[23.059823900040083, 113.33577366057769, 0.9768670377023632],
[23.04401161610398, 113.34870234800107, 1.0973053560927335],
[23.089294568045034, 113.40648681172956, 1.0672954423208023],
[22.945826689126843, 113.33555580590073, 1.0344023855972806],
[23.04111115355714, 113.17695539460153, 0.9788217131971989],
[23.06820848896712, 113.32961578569528, 1.024723822637628],
[23.16470166843589, 113.29593443086718, 1.0266264371321348],
[23.130655370411766, 113.29760988337169, 0.9288641272323043],
[23.1299580459227, 113.29052350896032, 0.9338362948257889],
[23.06675875854123, 113.29923343715164, 0.9908695977885447],
[23.02809171977561, 113.34476257559587, 0.9474159547995182],
[23.136822861421464, 113.31225617272405, 0.9095865953532773],
[23.12122913804494, 113.31572937900808, 0.9928438356557356],
[23.049598194789308, 113.3790651920976, 1.06995431249201],
[23.048634917456727, 113.27009885590954, 1.039837070566207],
[23.033238254710025, 113.24622685241481, 0.9528717872341834],
[23.055223681088982, 113.31703726488796, 0.9269648158630294],
[23.111763865814094, 113.330714652435, 1.0081795149804298],
[23.159514940356463, 113.3827896710709, 1.0125560088552084],
[23.015485603424164, 113.37168997940708, 0.8901657271145049],
[23.090062941586222, 113.31442240303598, 1.0437725746635351],
[23.10047396900036, 113.26930355685936, 1.056195916419667],
[23.086397403228723, 113.29500972551187, 0.9215703922399998],
[23.07181575355332, 113.27191797654557, 0.9771365604813234],
[23.082061644828187, 113.24971311589125, 0.9821751047997827],
[23.11405362091158, 113.28946835987047, 0.9944232666304159],
[23.174221426693236, 113.27016329800094, 0.9280187680354476],
[23.065415541299505, 113.34002676353906, 1.0593466485224678],
[23.063303058891776, 113.33684793920631, 0.9667912491794345],
[23.140455345891258, 113.29750672964703, 1.0325443825426526],
[23.156996865473428, 113.21065182613407, 0.99585894771756],
[23.038011746216338, 113.28391566408796, 1.0228530218334027],
[23.0589002853561, 113.41590655916437, 0.9999214835993677],
[23.17138554578974, 113.38701118194078, 0.9850042161036289],
[23.03373035184639, 113.28016118131181, 0.975209717968077],
[23.113778442614173, 113.29613019680484, 0.9672118741049642],
[23.074381748014467, 113.33745430210156, 1.019546702641773],
[23.067943665429517, 113.25848873955499, 0.9433265035275158],
[23.170215842016887, 113.28495632846364, 1.0096770568309708],
[23.067720545643997, 113.32999565199998, 0.9607360926143252],
[23.029012302732987, 113.30578275270763, 0.9672616324072332],
[23.139783201308784, 113.25740272595408, 0.931829158073609],
[23.13445946376369, 113.18465556205734, 0.9933079174440711],
[23.145146433696386, 113.26296146926379, 0.8997795925437833],
[23.102690986633903, 113.20091171665314, 1.0082426316059916],
[23.069118097622297, 113.30415096230118, 0.9864598087796809],
[23.086958138539327, 113.32280016523009, 1.014023187927512],
[23.162865123714795, 113.36267068030831, 0.9309042465674608],
[23.03848251916326, 113.38267607197582, 0.9745923728996312],
[23.047355022919856, 113.35134423231115, 1.077505487253896],
[23.081906724336644, 113.26406931003574, 1.0249938223041481],
[23.1019448944326, 113.25728707102219, 0.9605602425468109],
[23.05656609272457, 113.37055769425989, 1.0513148030074184],
[23.137824499025943, 113.37660944247588, 1.0213143234556135],
[23.18100685010041, 113.26796434313987, 0.9676950648284746],
[23.08486875700642, 113.26163942778828, 1.0425312384465804],
[23.069705949158397, 113.30261580785015, 0.9676064491953289],
[23.094955179678305, 113.3636746167014, 1.1034452276613536],
[23.051051722427978, 113.40340414824736, 0.9605732629926806],
[22.999719671183087, 113.25752059900128, 1.140748598965253],
[23.060876728946333, 113.28822670935287, 0.9455912109436538],
[23.143157080024576, 113.31652806597364, 0.9897672504095643],
[23.09760293550695, 113.33698646088699, 0.9450511327056311],
[23.129991297730452, 113.25342782957449, 0.9393332792480567],
[22.982729928393535, 113.29058769396606, 0.8962222376027345],
[23.040320067325865, 113.2388214245268, 0.9811546407868701],
[23.10123708337022, 113.31970396964944, 0.8871928935919335],
[23.057278485695278, 113.21240448164177, 1.107355418814855],
[23.099095555477987, 113.31728391870408, 0.9596210597979108],
[23.05220912306624, 113.30810341654148, 0.9381472905560007],
[23.04573475178744, 113.40729979798776, 0.9733943001883945],
[23.061146633710784, 113.31108527381849, 0.9829948186800115],
[23.09851117127804, 113.30790134844794, 1.0162554723402633],
[23.170017690282346, 113.32826990699701, 1.0345083284089052],
[23.07146333313078, 113.32748676968639, 0.9745144000946859],
[23.104647004323915, 113.34822637709222, 1.0705690175611116],
[23.11341387636721, 113.21919049891045, 1.0210252443867878],
[23.047677626049417, 113.32557356687758, 0.9966251510825506],
[23.057187771631714, 113.2927998114717, 0.9436919906172835],
[23.14200334833561, 113.27865345712694, 0.9563501656180777],
[23.030684371370953, 113.27724068377748, 1.0016042886318273],
[23.107204063174848, 113.3441113671808, 0.9798827824266236],
[23.061240695729083, 113.26547770816381, 0.9104500446504628],
[23.11406332938526, 113.34433537304587, 1.0066496259339799],
[23.0771963391058, 113.26019265161098, 0.9888784371231408],
[23.077298451018603, 113.28224950682745, 0.9889312516533824],
[23.1678222307225, 113.2408723050003, 1.0807849616426077],
[23.026728267722447, 113.22988606591832, 0.9726622404871234],
[23.150472403014202, 113.32160562093566, 0.9668452753603307],
[23.10804509368375, 113.35100338998645, 1.0143406805371933],
[23.011618659589534, 113.27685996012794, 1.0381139248485423],
[23.089911317481743, 113.25536623988076, 0.97591495322806],
[23.074803700299842, 113.21018729287981, 1.0812702432178511],
[23.0602257785443, 113.35655311401517, 1.003301754421832],
[23.109792776732405, 113.34223141500799, 0.9714383257695531],
[23.109974548641983, 113.29663226861786, 1.0988404154512421],
[23.076653178993055, 113.2903620248449, 1.0747781827801373],
[23.066006819026672, 113.39154904272304, 0.8866985950738957],
[23.06187011789522, 113.329274847491, 0.9531444209855836],
[23.09215722733291, 113.33500394073502, 0.9979606393446608],
[23.108783716927473, 113.41311896034186, 1.0783989692033518],
[23.051587608702615, 113.27271074466547, 1.0126534377897292],
[23.08112625981011, 113.34069103959165, 1.0003259426212479],
[23.09298487152137, 113.24428845541146, 1.0114063444382395],
[23.096636180424216, 113.32259280126202, 0.9559099236091522],
[23.0709517069991, 113.2105009352563, 1.056331817902246],
[23.12751518041026, 113.40032450273308, 1.1028222249971276],
[23.201596091357615, 113.23470170525503, 0.9631116984534311],
[22.983921730495997, 113.30921051331782, 0.9542044693906839],
[23.13908838982864, 113.27736158913784, 1.0293151459148855],
[23.091292604822932, 113.27072968232939, 0.9467253935742427],
[23.11243915411646, 113.31074953589068, 1.063458515813779],
[23.18282414038781, 113.2594498807081, 1.094745961417506],
[23.124390198834625, 113.21274418888906, 1.0051802690222171],
[23.20289473916005, 113.33532217174167, 0.9562736851382847],
[23.1338188440322, 113.21589166408948, 0.9992927625602154],
[23.124782037583262, 113.31429206976628, 0.9798818993761326],
[23.063475022354456, 113.23145341339422, 1.0037239663998783],
[23.037929084538124, 113.36982291778386, 0.9566332092393617],
[23.164798012245267, 113.27927654826411, 0.9519859276080208],
[23.05682563583087, 113.30348737007574, 0.876547437225224],
[23.22814127031294, 113.29614688200238, 1.0002345771830492],
[23.112337645383587, 113.26396100167692, 1.0075606958772216],
[23.133193003264992, 113.25938134990102, 1.0001811516254056],
[22.96985992733565, 113.24540021833023, 0.951494630982537],
[23.0599286111793, 113.36433002022495, 1.0633314275589105],
[23.022383968315925, 113.38705523706578, 0.9361622235332209],
[23.135965935419875, 113.35963882798059, 1.0356457318609125],
[23.119490421252756, 113.23357045072092, 0.9501147001693675],
[23.1121715603478, 113.379355886597, 0.9450458197698964],
[23.08659916923502, 113.25445740530346, 0.9656327236391073],
[23.187975592516526, 113.34894333608283, 1.0225955580246116],
[23.174693976281468, 113.3146175424754, 1.028027624165148],
[23.033120849321246, 113.34638920258789, 1.064646065061616],
[23.06364842128837, 113.2564567834396, 1.048962215241505],
[23.1165423647422, 113.22475265125878, 0.971290594252817],
[23.132360226911942, 113.31003300379429, 0.9053035246691227],
[22.971457470232167, 113.27045265694564, 1.0192232335525084],
[23.09583254755467, 113.35974778015907, 0.9802258814445237],
[23.081395347061516, 113.28785519320817, 1.0251467613861796],
[23.146684915488716, 113.23140841140338, 0.922242725025256],
[23.03449389997694, 113.33237660619729, 0.9747125337496771],
[23.09517536813799, 113.34188223683267, 0.8794253875133888],
[23.091801954347964, 113.27297205884416, 0.951116021291397],
[23.081736673630974, 113.22836764928176, 1.050614746862662],
[23.11737790384634, 113.32435149374318, 0.9579633801677969],
[23.115768801617623, 113.28584560714927, 0.962981547252387],
[23.044850353496773, 113.35725144638734, 0.9929068777302035],
[23.09387782241977, 113.2558852069788, 1.0610866248514026],
[23.090925471899908, 113.40708706190583, 1.0213397343133817],
[23.055196968384735, 113.35077244301102, 1.0082878095134529],
[23.09765606569975, 113.25954840216448, 0.9712221999872299],
[23.124012119137152, 113.30807697246041, 0.9749031453223709],
[23.009727529268947, 113.19040387274985, 1.0379639114632377],
[23.10001395284033, 113.28747637330294, 0.9570058783914028],
[23.014572534806238, 113.39872222828662, 0.9224812967866087],
[23.07164522022995, 113.29889814680485, 0.9327168732976062],
[23.01018365235604, 113.29741155022909, 0.9881200106951283],
[23.137736776915073, 113.28645903280649, 0.9747978631538995],
[23.110167732851284, 113.21622307964186, 0.9626972725343087],
[23.07988530984633, 113.32042015008516, 0.9968664674689669],
[23.13218200197872, 113.23997081058624, 1.1112442673324139],
[23.037958935150062, 113.34953782521065, 1.0568375040575133],
[23.00258933385775, 113.29521946145935, 0.9923533050846497],
[22.99500366218763, 113.24697403651301, 1.0747148274953087],
[23.101269523033718, 113.26627385906585, 1.1585747242869329],
[23.056907707790835, 113.32364639473913, 0.9840376338944937],
[23.09766136616909, 113.29146312158494, 0.977209916201389],
[23.10566443354946, 113.31365735821936, 0.9632676123505158],
[23.06488068135747, 113.28208969919801, 0.9538044549560156],
[23.09563083615826, 113.29870977158149, 0.9074605325437103],
[23.096463726724775, 113.24795742271954, 0.9884978400779303],
[23.084297301650086, 113.24860556357082, 1.0525404605482656],
[23.11105550299495, 113.24446903885507, 1.0221646946782512],
[23.071566092065293, 113.22453258501857, 1.0503125037667875],
[23.06960951703729, 113.23378700517061, 1.044900348882142],
[23.130719218203776, 113.31953370185602, 0.9996287949576063],
[23.03112919824916, 113.3405694784075, 0.8656620974664794],
[23.117407666993948, 113.21479000784922, 0.82408691259813],
[23.02401494112507, 113.3543676556085, 1.0490127724904623],
[23.0523612476613, 113.36598442520975, 0.9512744778441958],
[23.11562477051131, 113.27900507513928, 0.9836612199847468],
[23.065294517554626, 113.33172458280367, 0.9539224247205167],
[23.106441639260403, 113.2406889320042, 1.0337442356174757],
[23.01002305085585, 113.19105233814925, 0.9812985669879906],
[23.16699080983821, 113.26956759674246, 1.0410340776043785],
[23.12737946419699, 113.26680070685237, 0.9806937951959852],
[23.053700797281152, 113.3364897616824, 1.0324335040210815],
[23.0300604809055, 113.27740444123657, 0.963145386349546],
[23.160867148166215, 113.35988825149546, 0.9986512849632265],
[23.087078292019775, 113.27332694280972, 1.016882367943754],
[22.98442906835672, 113.31708083462408, 0.9413037776585411],
[23.107705509824882, 113.31158926726981, 0.9361983698889469],
[22.997215755947845, 113.28806263832456, 1.0174447450127926]]
# HeatMap(热力图)
from folium.plugins import HeatMap
# 创建地图(使用默认 OpenStreetMap 底图)
m = folium.Map([23.087, 113.3], zoom_start=12)
# 添加热力图层
HeatMap(data).add_to(m)
# 保存为 HTML 文件
m.save('Heatmap.html')
print("热力图已保存到 Heatmap.html")
# 显示地图
m热力图已保存到 Heatmap.html
新版本 folium 推荐使用以下方式指定底图:
# 默认 OpenStreetMap(推荐)
folium.Map(location=[lat, lon])
# 使用其他底图提供商
folium.Map(location=[lat, lon], tiles='OpenStreetMap')
folium.Map(location=[lat, lon], tiles='CartoDB positron')
folium.Map(location=[lat, lon], tiles='CartoDB dark_matter')注意:Stamen 底图已不再免费提供,建议使用 OpenStreetMap 或 CartoDB。