How can multiple charts be drawn on a canvas in Python?

In Python, matplotlib library can be used to plot multiple charts. Below is a simple example demonstrating how to plot multiple charts on a canvas.

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()

In this example, we first create a canvas containing a 2 by 2 grid of subplots using the plt.subplots() function. Next, we select a specific subplot by indexing axes[row index, column index] and use commonly used plotting functions (such as plot(), bar(), scatter(), hist(), etc.) to draw the actual charts in the subplot. Finally, we adjust the spacing between subplots using the plt.tight_layout() function and display the chart using the plt.show() function.

You can draw different charts in different subplots according to your needs.

bannerAds