Pythonでキャンバス上に複数のグラフを描画する方法は何ですか?
Pythonで、matplotlibライブラリを使用して複数のグラフを描画することができます。以下は、複数のグラフを描画する方法を示す簡単な例です。
import matplotlib.pyplot as plt
# 创建画布和子图
fig, axes = plt.subplots(nrows=2, ncols=2)
# 在子图中绘制图表
axes[0, 0].plot([1, 2, 3, 4], [1, 4, 9, 16])
axes[0, 0].set_title('Plot 1')
axes[0, 1].bar(['A', 'B', 'C'], [3, 7, 2])
axes[0, 1].set_title('Plot 2')
axes[1, 0].scatter([1, 2, 3, 4], [1, 4, 9, 16])
axes[1, 0].set_title('Plot 3')
axes[1, 1].hist([1, 2, 3, 1, 2, 3, 1, 2, 3])
axes[1, 1].set_title('Plot 4')
# 调整子图之间的间距
plt.tight_layout()
# 显示图表
plt.show()
この例では、plt.subplots()関数を使用して、2行2列のサブプロットを含むキャンバスを作成します。次に、axes[行のインデックス, 列のインデックス]を使用してサブプロットを選択し、plot()、bar()、scatter()、hist()などの一般的なプロット関数を使用して具体的な図を描画します。最後に、plt.tight_layout()関数を使用してサブプロット間のスペースを調整し、plt.show()関数を使用して図を表示します。
必要に応じて異なるグラフを異なるサブプロットに描画することができます。