Home > Backend Development > Python Tutorial > Quick Learning: Drawing Heatmaps and Scatterplots with Python

Quick Learning: Drawing Heatmaps and Scatterplots with Python

WBOY
Release: 2023-09-28 14:58:47
Original
1040 people have browsed it

Quick Learning: Drawing Heatmaps and Scatterplots with Python

Quick Learning: Using Python to draw heat maps and scatter plots (with code examples)

Introduction:
In data visualization, heat maps and scatter plots Graphs are two common chart types. Heat maps can visually display the distribution and changing trends of data, while scatter plots are suitable for showing the correlation between multiple data points. This article will introduce how to use Python to draw these two charts and give specific code examples.

1. Draw a heat map

  1. Prepare data
    Drawing a heat map requires preparing a two-dimensional array (matrix) as input data. The value of each element represents the color depth or heat of the location. The following is a simple example, using the numpy library to generate a 3x3 random matrix as input data:
import numpy as np

data = np.random.rand(3, 3)
Copy after login
  1. Drawing a heat map
    Use the imshow function in the matplotlib library to draw a heat map, This function accepts a two-dimensional array as input data and can automatically determine the depth of the color based on the value of the data. Here is a simple example:
import matplotlib.pyplot as plt

plt.imshow(data, cmap='hot', interpolation='nearest')
plt.colorbar()  # 添加颜色渐变条
plt.show()
Copy after login

In the above code, a hot color map is used to map smaller values ​​​​to bright yellow and larger values ​​​​to dark red, and is specified using the interpolation parameter interpolation method.

2. Drawing a scatter plot

  1. Preparing data
    Drawing a scatter plot requires preparing two one-dimensional arrays, representing the x-coordinates and y-coordinates of the data points respectively. Here is a simple example using the numpy library to generate a random set of data points:
import numpy as np

x = np.random.rand(100)
y = np.random.rand(100)
Copy after login
  1. Plot a scatter plot
    Use the scatter function in the matplotlib library to plot a scatter plot, This function accepts two one-dimensional arrays as input data, representing the x-coordinate and y-coordinate of the data point respectively. The following is a simple example:
import matplotlib.pyplot as plt

plt.scatter(x, y, marker='o', c='r')  # 使用红色的圆点表示散点图
plt.xlabel('X')  # 设置x轴标签
plt.ylabel('Y')  # 设置y轴标签
plt.title('Scatter Plot')  # 设置图表标题
plt.show()
Copy after login

In the above code, use the marker parameter to specify the marker shape of the scatter points, and the c parameter to specify the color of the scatter points.

Conclusion:
This article introduces the method of using Python to draw heat maps and scatter plots, and gives specific code examples. By studying these sample codes, readers can quickly start drawing heat maps and scatter plots, and conduct visual analysis of data. At the same time, readers can also conduct secondary development and optimization according to their own needs to achieve more personalized data visualization effects.

The above is the detailed content of Quick Learning: Drawing Heatmaps and Scatterplots with Python. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template