Skip to content

Latest commit

 

History

History
74 lines (61 loc) · 2.44 KB

File metadata and controls

74 lines (61 loc) · 2.44 KB
jupyter
jupytext kernelspec language_info plotly
notebook_metadata_filter text_representation
all
extension format_name format_version jupytext_version
.md
markdown
1.3
1.16.3
display_name language name
Python 3 (ipykernel)
python
python3
codemirror_mode file_extension mimetype name nbconvert_exporter pygments_lexer version
name version
ipython
3
.py
text/x-python
python
python
ipython3
3.10.0
description display_as language layout name order page_type permalink redirect_from thumbnail
How to make a density heatmap in Python with Plotly.
maps
python
base
Density Heatmap
6
u-guide
python/density-heatmaps/
python/mapbox-density-heatmaps/
thumbnail/map-density.png

Density map with plotly.express

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures.

With px.density_map, each row of the DataFrame is represented as a point smoothed with a given radius of influence.

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')

import plotly.express as px
fig = px.density_map(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=10,
                        center=dict(lat=0, lon=180), zoom=0,
                        map_style="open-street-map")
fig.show()

Density map with plotly.graph_objects

If Plotly Express does not provide a good starting point, it is also possible to use the more generic go.Densitymap class from plotly.graph_objects.

import pandas as pd
quakes = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')

import plotly.graph_objects as go
fig = go.Figure(go.Densitymap(lat=quakes.Latitude, lon=quakes.Longitude, z=quakes.Magnitude,
                                 radius=10))
fig.update_layout(map_style="open-street-map", map_center_lon=180)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Reference

See function reference for px.(density_map) or https://plotly.com/python/reference/densitymap/ for available attribute options.