matplotlib同时画多个图
时间: 2023-07-04 10:06:41 浏览: 54
在Matplotlib中,可以使用`subplot()`函数在同一画布上绘制多个子图。`subplot()`函数的第一个参数指定子图的行数,第二个参数指定子图的列数,第三个参数指定子图的索引(从左上角开始,从左到右,从上到下编号),用于指定当前要绘制的子图。
以下是一个例子:
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# 创建画布并设置子图
fig, axs = plt.subplots(2, 1)
# 绘制第一个子图
axs[0].plot(x, y1)
axs[0].set_title('sin(x)')
# 绘制第二个子图
axs[1].plot(x, y2)
axs[1].set_title('cos(x)')
# 显示图像
plt.show()
```
这个例子中,我们创建了两个子图,一个绘制`sine`曲线,另一个绘制`cosine`曲线。使用`subplots()`函数创建画布,并将画布分成两行一列的子图布局。然后,我们在每个子图中使用`plot()`函数绘制曲线,并使用`set_title()`函数设置图像标题。最后,我们使用`show()`函数显示图像。
你可以根据需要调整子图的行数和列数,以及每个子图的位置和大小。
相关问题
pandas matplotlib同时画多个图
使用matplotlib中的subplot函数可以在同一画布上绘制多个图,而pandas中的plot函数可以方便地将数据绘制成图形。
以下是一个示例代码,展示如何使用pandas和matplotlib同时绘制多个图:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据
df = pd.read_csv('data.csv')
# 创建figure对象,并设置图像大小
fig = plt.figure(figsize=(10, 8))
# 在figure对象上创建多个子图
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)
# 绘制第一个子图
df.plot(x='date', y='value1', ax=ax1)
ax1.set_title('Value 1')
# 绘制第二个子图
df.plot(x='date', y='value2', ax=ax2)
ax2.set_title('Value 2')
# 绘制第三个子图
df.plot(x='date', y='value3', ax=ax3)
ax3.set_title('Value 3')
# 绘制第四个子图
df.plot(x='date', y='value4', ax=ax4)
ax4.set_title('Value 4')
# 调整子图之间的间距
plt.subplots_adjust(wspace=0.3, hspace=0.3)
# 显示图像
plt.show()
```
代码中,首先读取一个包含四列数据的CSV文件,然后创建了一个10x8的画布,并使用subplot函数创建了四个子图。接下来,使用pandas的plot函数在每个子图上绘制一列数据,并在每个子图上设置了标题。最后,使用subplots_adjust函数调整子图之间的间距,然后显示图像。
matplotlib如何画多个矩形
要画多个矩形,可以使用Matplotlib中的`Rectangle`对象和`add_patch`方法。以下是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
# 创建一个Figure对象和一个Axes对象
fig, ax = plt.subplots()
# 创建多个Rectangle对象并添加到Axes对象中
rect1 = Rectangle((0.2, 0.2), 0.5, 0.3, color='r')
rect2 = Rectangle((0.5, 0.5), 0.3, 0.4, color='g')
rect3 = Rectangle((0.1, 0.6), 0.4, 0.2, color='b')
ax.add_patch(rect1)
ax.add_patch(rect2)
ax.add_patch(rect3)
# 设置坐标轴范围和标题
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_title('Multiple Rectangles')
# 显示图形
plt.show()
```
这个代码将在一个图形中画出三个矩形,每个矩形的位置、大小和颜色都不同。你可以根据需要修改代码来画出你想要的矩形。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)