演示如何使用Python中的Matplotlib库绘制各种数据可视化图表,包括折线图、柱状图和热力图:

import matplotlib.pyplot as plt
import numpy as np

# 生成示例数据
x = np.linspace(0, 10, 100)
y = np.sin(x)

# 绘制折线图
plt.figure(figsize=(8, 6))
plt.plot(x, y)
plt.title('折线图')
plt.xlabel('X轴')
plt.ylabel('Y轴')
plt.grid(True)
plt.show()

# 生成示例数据
categories = ['A', 'B', 'C', 'D', 'E']
values = [25, 40, 30, 35, 20]

# 绘制柱状图
plt.figure(figsize=(8, 6))
plt.bar(categories, values)
plt.title('柱状图')
plt.xlabel('类别')
plt.ylabel('数值')
plt.show()

# 生成示例数据
data = np.random.rand(10, 10)

# 绘制热力图
plt.figure(figsize=(8, 6))
plt.imshow(data, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.title('热力图')
plt.show()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

在这个示例中,我们首先使用NumPy库生成了一些示例数据,分别用于绘制折线图、柱状图和热力图。然后,我们使用Matplotlib库的不同函数来绘制这些图表。在绘制折线图和柱状图时,我们使用了plotbar函数,并设置了相应的标题、坐标轴标签和网格。在绘制热力图时,我们使用了imshow函数,并设置了颜色映射和标题。