Home >Backend Development >Python Tutorial >Data visualization example in Python: heat map
With the advent of the data era, data visualization has increasingly become an indispensable part of data analysis. In Python, there are rich visualization tool libraries, such as Matplotlib, Seaborn, etc. This article mainly introduces one of the methods to implement heat maps, hoping to be helpful to readers in Python data visualization.
1. Introduction to Heat Map
Heat map, also known as density map, uses the depth of color to represent the density of data. In data visualization, heat maps provide a more intuitive presentation method and can clearly express the spatial distribution of data.
2. Implementation of heat map
In Python, we can use the heatmap function in the Seaborn library to draw heat maps.
The specific steps are as follows:
import numpy as np
import seaborn as sns
import matplotlib .pyplot as plt
data = np.random. rand(10, 10)
sns.heatmap(data, cmap=' coolwarm')
In it, the cmap parameter specifies the color settings of the heat map. Here we are using the coolwarm color scheme.
After running the above code, you can get a simple heat map.
3. Complete code
The following is a complete example code, which shows how to implement a more complete heat map. Including drawing coordinate axes, labels, etc.:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
data = np.random.rand(10, 10) * 10
heatmap = sns.heatmap(data, cmap='coolwarm')
heatmap.set_xlabel('X-label')
heatmap.set_ylabel('Y-label')
heatmap.set_title( 'Heatmap')
for i in range(len(data)):
for j in range(len(data[0])): plt.text(j + 0.5, i + 0.5, round(data[i][j], 2), ha="center", va="center", color="white")
plt.show()
Run the above code, we can get a heat map with a border, axis name, and scale label.
4. Summary
The Seaborn library in Python provides a fast method for drawing heat maps, and exquisite effects can be achieved through appropriate settings. Through the introduction of this article, readers can become more proficient in using Python visualization tools to display their own data.
The above is the detailed content of Data visualization example in Python: heat map. For more information, please follow other related articles on the PHP Chinese website!