44 lines
1.0 KiB
Plaintext
44 lines
1.0 KiB
Plaintext
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
|
|
# 准备数据
|
|
x = np.linspace(0, 10, 100) # 线形图数据
|
|
y = np.sin(x)
|
|
|
|
sizes = [27.6, 17.2, 24.1, 31.0] # 饼状图数据
|
|
labels = ['A', 'B', 'C', 'D']
|
|
|
|
bar_x = ['条形图1', '条形图2'] # 条形图数据
|
|
bar_y = [10, 40]
|
|
|
|
scatter_x = np.random.rand(20) # 散点图数据
|
|
scatter_y = np.random.rand(20)
|
|
|
|
# 创建画布和子图
|
|
fig, axs = plt.subplots(2, 2, figsize=(12, 10))
|
|
|
|
# 绘制线形图
|
|
axs[0, 0].plot(x, y, label='sin(x)', color='b')
|
|
axs[0, 0].set_title('线形图')
|
|
axs[0, 0].legend()
|
|
|
|
# 绘制饼状图
|
|
axs[0, 1].pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
|
|
axs[0, 1].set_title('饼状图')
|
|
|
|
# 绘制条形图
|
|
axs[1, 0].bar(bar_x, bar_y, color=['r', 'g'])
|
|
axs[1, 0].set_title('条形图')
|
|
axs[1, 0].set_ylabel('值')
|
|
|
|
# 绘制散点图
|
|
axs[1, 1].scatter(scatter_x, scatter_y, color='purple', alpha=0.5)
|
|
axs[1, 1].set_title('散点图')
|
|
axs[1, 1].set_xlabel('X轴')
|
|
axs[1, 1].set_ylabel('Y轴')
|
|
|
|
# 调整布局
|
|
plt.tight_layout()
|
|
|
|
# 显示图表
|
|
plt.show() |