You should first group your data by country and then by year and count number of crimes:

new_df = df.groupby(["country","year"])["occurrence"].count().reset_index()

After that, you should change the structure of the data by pivoting the data according to your needs:

new_df = new_df.pivot(index='country', columns='year')['occurrence'].fillna(0)

Now, you can plot your heatmap:

import plotly.express as px
fig = px.imshow(new_df, x=new_df.columns, y=new_df.index)
fig.update_layout(width=500,height=500)
fig.show()

Answer from Hamzah Al-Qadasi on Stack Overflow
๐ŸŒ
Plotly
plotly.com โ€บ python โ€บ heatmaps
Heatmaps in Python
Please refer to the 2D Histogram documentation for this kind of figure. 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.
๐ŸŒ
Plotly
plotly.com โ€บ python โ€บ annotated-heatmap
Annotated heatmaps in Python
import plotly.express as px import numpy as np np.random.seed(1) z = np.random.randn(20, 20) fig = px.imshow(z, text_auto=".2f", color_continuous_scale='Greys', aspect="auto") fig.show() px.imshow() can produce more elaborate annotated heatmaps.
๐ŸŒ
GitHub
github.com โ€บ plotly โ€บ plotly.py โ€บ blob โ€บ main โ€บ doc โ€บ python โ€บ heatmaps.md
plotly.py/doc/python/heatmaps.md at main ยท plotly/plotly.py
Please refer to the 2D Histogram documentation for this kind of figure. 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.
Author: plotly
๐ŸŒ
Plotly
community.plotly.com โ€บ ๐Ÿ“Š plotly python
Heatmaps in plotly express - ๐Ÿ“Š Plotly Python - Plotly Community Forum
November 28, 2019 - I want to quickly create heatmaps where I already have proper data for the all the dimensions and do not need any of the preprocessing that comes with px.density_heatmap. So for now it seems that I need to revert to graph_objects to accomplish that, since there is no px.heatmap.
๐ŸŒ
Plotly
plotly.com โ€บ python โ€บ density-heatmaps
Density heatmap in Python
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()
๐ŸŒ
Plotly
plotly.com โ€บ python-api-reference โ€บ generated โ€บ plotly.express.density_heatmap.html
plotly.express.density_heatmap โ€” 6.8.0 documentation
plotly.express.density_heatmap(data_frame=None, x=None, y=None, z=None, facet_row=None, facet_col=None, facet_col_wrap=0, facet_row_spacing=None, facet_col_spacing=None, hover_name=None, hover_data=None, animation_frame=None, animation_group=None, category_orders=None, labels=None, orientation=None, color_continuous_scale=None, range_color=None, color_continuous_midpoint=None, marginal_x=None, marginal_y=None, opacity=None, log_x=False, log_y=False, range_x=None, range_y=None, histfunc=None, histnorm=None, nbinsx=None, nbinsy=None, text_auto=False, title=None, subtitle=None, template=None, width=None, height=None) โ†’ plotly.graph_objects._figure.Figureยถ ยท
๐ŸŒ
PYTHON CHARTS
python-charts.com โ€บ home โ€บ correlation โ€บ heatmaps in plotly with imshow
Heatmaps in plotly with imshow | PYTHON CHARTS
October 23, 2022 - import plotly.express as px # Sample data data = [[5, 23, 15, 47], [25, 5, 25, 12], [31, 35, 25, 41], [12, 18, 5, 7]] fig = px.imshow(data, text_auto = True, x = ['A', 'B', 'C', 'D'], y = ['V1', 'V2', 'V3', 'V4']) fig.show() The default color palette of the plotly heatmaps can be overriden through the color_continuous_scale argument.
Find elsewhere
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ creating-a-density-heatmap-plot-with-plotly-express-in-python
Creating a density heatmap plot with Plotly Express in Python
Density estimation: Plotly Express uses kernel density estimation (KDE) to estimate the density of data points in each plot region. This estimation is used to generate the heatmap, where more intense colors represent areas of higher density.
๐ŸŒ
Plotly
plotly.github.io โ€บ plotly.py-docs โ€บ generated โ€บ plotly.graph_objects.Heatmap.html
plotly.graph_objects.Heatmap โ€” 7.0.0 documentation
Then, by default, the resulting heatmap will have N partitions along the y axis and M partitions along the x axis. In other words, the i-th row/ j-th column cell in z is mapped to the i-th partition of the y axis (starting from the bottom of the plot) and the j-th partition of the x-axis (starting from the left of the plot).
๐ŸŒ
Kanaries
docs.kanaries.net โ€บ topics โ€บ Plotly โ€บ plotly-heatmap
Plotly Heatmap - Tips, Tricks, and Examples" โ€“ Kanaries
August 17, 2023 - In this example, we use the imshow() function from Plotly Express to create a heatmap.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Creating Geospatial Heatmaps With Plotly Express MapBox and Folium in Python - Data Visualisation - YouTube
Heatmaps, also known as Density Maps, are data visualisations that display the spatial distribution of a variable across a geographic area. They can be great...
Published: November 8, 2023
๐ŸŒ
Plotly
plotly.com โ€บ python โ€บ 2D-Histogram
2d histograms in Python
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. The Plotly Express function density_heatmap() can be used to produce density heatmaps.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ plotly โ€บ plotly heatmap
How to Plot Heatmap in Plotly | Delft Stack
February 2, 2024 - import plotly.express as px data = [[1, 10, 20], [30, 1, 10], [20, 30, 1]] fig = px.imshow(data) fig.show() ... The imshow() function gave each triangle a different color according to its value using the default color sequence. We can change the default color sequence using the color_continuous_scale argument and setting its value to a supported color sequence name like hot and HSV. To convert the heatmap chart into a group of subplots containing a single column in each subplot, we can use the facet_col for columns and set its value to a list of integers or strings used to set the name of the subplot.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-create-a-heatmap-plot-with-plotly-graph-objects-in-python
How to create a heatmap plot with Plotly Graph Objects in Python
Line 6: Loads the Iris dataset using Plotly Express's built-in sample dataset. Line 9: Prints the first five rows of the loaded dataset using the head() method to inspect the data. Lines 12โ€“19: Creates a heatmap trace using Plotly Graph Objects (go.Heatmap).
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ plotly โ€บ plotly_heatmap.htm
Plotly - Heatmap
Heat Maps are innately self-explanatory. The darker the shade, the greater the quantity (the higher the value, the tighter the dispersion, etc.). Plotlys graph_objects module contains Heatmap() function. It needs x, y and z attributes.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python:plotly โ€บ express โ€บ .density_heatmap()
Python:Plotly | express | .density_heatmap() | Codecademy
February 25, 2025 - Creates a 2D histogram-based heatmap that visualizes the density of points in a dataset using color intensity.
๐ŸŒ
HatchJS
hatchjs.com โ€บ home โ€บ how to create a plotly heatmap from a dataframe in python
How to Create a Plotly Heatmap from a DataFrame in Python
January 5, 2024 - To create a heatmap from a dataframe in Python, you can use the `plotly.express.heatmap()` function.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Heatmap - How to Create Heatmap in Plotly Python - YouTube
Learn How to create Heatmap in Plotly Python. How to create a heatmap in Python. How to Create Heatmap with Plotly library in Python.
Published: March 29, 2024
Views: 702