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
import numpy as np data = np.random.rand(3, 3)
import matplotlib.pyplot as plt plt.imshow(data, cmap='hot', interpolation='nearest') plt.colorbar() # 添加颜色渐变条 plt.show()
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
import numpy as np x = np.random.rand(100) y = np.random.rand(100)
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()
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!