pandas.plot()子图间距
时间: 2023-10-19 21:11:12 浏览: 59
在使用pandas的plot()函数绘制子图时,可以通过figsize和subplots_adjust参数来调整子图的间距。具体来说,figsize参数可以控制整个图形的大小,subplots_adjust参数可以控制子图之间的间距。
下面是一个例子:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 生成一些随机数据
data = pd.DataFrame(np.random.rand(4, 3), columns=['A', 'B', 'C'])
# 绘制子图
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 6))
data.plot(ax=axes[0, 0])
data.plot(ax=axes[0, 1])
data.plot(ax=axes[1, 0])
data.plot(ax=axes[1, 1])
# 调整子图之间的间距
plt.subplots_adjust(wspace=0.3, hspace=0.5)
```
在这个例子中,我们使用了subplots()函数生成了一个2x2的子图,并将每个子图上绘制相同的随机数据。我们还使用了figsize参数来控制整个图形的大小,并使用subplots_adjust()函数来调整子图之间的间距。在这个例子中,我们将水平方向上的间距设置为0.3,将垂直方向上的间距设置为0.5。
相关问题
import numpy as np import pandas as pd import matplotlib.pyplot as plt import pyhht # 读取csv文件数据 data = pd.read_csv('77.csv', header=None) time = data.iloc[:, 0].values charge = data.iloc[:, 1].values # EMD分解 decomposer = pyhht.EMD() imfs = decomposer.decompose(charge)fig, axs = plt.subplots(nrows=imfs.shape[0]+2, ncols=1, figsize=(10, 15)) axs[0].plot(time, charge, label='Original') axs[0].set_xlabel('Time') axs[0].set_ylabel('Charge') axs[0].legend() for i in range(imfs.shape[0]): axs[i+1].plot(time, imfs[i], label=f'IMF{i+1}') axs[i+1].set_xlabel('Time') axs[i+1].set_ylabel('Charge') axs[i+1].legend() residuals = charge - np.sum(imfs, axis=0) axs[imfs.shape[0]+1].plot(time, residuals, label='Residuals') axs[imfs.shape[0]+1].set_xlabel('Time') axs[imfs.shape[0]+1].set_ylabel('Charge') axs[imfs.shape[0]+1].legend() axs[0].set_title('Original Signal') for i in range(imfs.shape[0]): axs[i+1].set_title(f'IMF {i+1}') axs[imfs.shape[0]+1].set_title('Residuals') plt.tight_layout() plt.show()调整以上代码,使得子图之间间距略大
可以使用`plt.subplots_adjust()`函数来调整子图之间的间距,例如在`plt.tight_layout()`函数之前添加以下代码即可:
```python
plt.subplots_adjust(hspace=0.5)
```
其中`hspace`参数控制水平方向的间距,默认值为0.2,可以根据需要进行调整。完整代码如下:
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pyhht
# 读取csv文件数据
data = pd.read_csv('77.csv', header=None)
time = data.iloc[:, 0].values
charge = data.iloc[:, 1].values
# EMD分解
decomposer = pyhht.EMD()
imfs = decomposer.decompose(charge)
fig, axs = plt.subplots(nrows=imfs.shape[0]+2, ncols=1, figsize=(10, 15))
axs[0].plot(time, charge, label='Original')
axs[0].set_xlabel('Time')
axs[0].set_ylabel('Charge')
axs[0].legend()
for i in range(imfs.shape[0]):
axs[i+1].plot(time, imfs[i], label=f'IMF{i+1}')
axs[i+1].set_xlabel('Time')
axs[i+1].set_ylabel('Charge')
axs[i+1].legend()
residuals = charge - np.sum(imfs, axis=0)
axs[imfs.shape[0]+1].plot(time, residuals, label='Residuals')
axs[imfs.shape[0]+1].set_xlabel('Time')
axs[imfs.shape[0]+1].set_ylabel('Charge')
axs[imfs.shape[0]+1].legend()
axs[0].set_title('Original Signal')
for i in range(imfs.shape[0]):
axs[i+1].set_title(f'IMF {i+1}')
axs[imfs.shape[0]+1].set_title('Residuals')
plt.subplots_adjust(hspace=0.5)
plt.tight_layout()
plt.show()
```
import numpy as np import pandas as pd import matplotlib.pyplot as plt import pyhht # 读取csv文件数据 data = pd.read_csv('77.csv', header=None) time = data.iloc[:, 0].values charge = data.iloc[:, 1].values # EMD分解 decomposer = pyhht.EMD(charge) imfs = decomposer.decompose() # 绘制原始序列和分解后的IMF分量 plt.figure(figsize=(10, 6)) plt.plot(time, charge, label='Original') plt.xlabel('Time') plt.ylabel('Charge') plt.legend() plt.show() for i in range(imfs.shape[0]): plt.plot(time, imfs[i], label=f'IMF{i+1}') plt.xlabel('Time') plt.ylabel('Charge') plt.legend() plt.show() residuals = charge - np.sum(imfs, axis=0) plt.plot(time, residuals, label='Residuals') plt.xlabel('Time') plt.ylabel('Charge') plt.legend() plt.show()在上述代码后增写一段代码,使得上述代码中绘制的图片可以显示在一张图中
可以使用 subplots() 方法将多个图像显示在同一个图中。以下是修改后的代码:
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pyhht
# 读取csv文件数据
data = pd.read_csv('77.csv', header=None)
time = data.iloc[:, 0].values
charge = data.iloc[:, 1].values
# EMD分解
decomposer = pyhht.EMD(charge)
imfs = decomposer.decompose()
# 绘制原始序列和分解后的IMF分量
fig, axs = plt.subplots(nrows=imfs.shape[0]+1, ncols=1, figsize=(10, 10))
axs[0].plot(time, charge, label='Original')
axs[0].set_xlabel('Time')
axs[0].set_ylabel('Charge')
axs[0].legend()
for i in range(imfs.shape[0]):
axs[i+1].plot(time, imfs[i], label=f'IMF{i+1}')
axs[i+1].set_xlabel('Time')
axs[i+1].set_ylabel('Charge')
axs[i+1].legend()
plt.tight_layout()
plt.show()
residuals = charge - np.sum(imfs, axis=0)
plt.plot(time, residuals, label='Residuals')
plt.xlabel('Time')
plt.ylabel('Charge')
plt.legend()
plt.show()
```
这样就可以将每个 IMF 和原始信号一起显示在同一个图中。`tight_layout()` 方法可以自动调整子图之间的间距,使它们更紧凑。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)