python数据可视化直方图一次性画四幅
时间: 2023-06-12 22:07:46 浏览: 105
可以使用 Matplotlib 库来画出四幅直方图。以下是一个示例代码:
```python
import matplotlib.pyplot as plt
import numpy as np
# 生成随机数据
data1 = np.random.normal(0, 1, 1000)
data2 = np.random.normal(2, 1, 1000)
data3 = np.random.normal(5, 2, 1000)
data4 = np.random.normal(-2, 0.5, 1000)
# 创建画布和子图
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
# 画第一幅直方图
axs[0, 0].hist(data1, bins=20, color='skyblue')
axs[0, 0].set_title('Data 1')
# 画第二幅直方图
axs[0, 1].hist(data2, bins=20, color='orange')
axs[0, 1].set_title('Data 2')
# 画第三幅直方图
axs[1, 0].hist(data3, bins=20, color='green')
axs[1, 0].set_title('Data 3')
# 画第四幅直方图
axs[1, 1].hist(data4, bins=20, color='pink')
axs[1, 1].set_title('Data 4')
# 调整子图之间的间距
fig.tight_layout()
# 显示图像
plt.show()
```
运行上述代码,即可得到四幅直方图的画布。其中,每幅直方图的数据分别为 `data1`,`data2`,`data3` 和 `data4`。每幅直方图的标题分别为 Data 1,Data 2,Data 3 和 Data 4。你可以根据自己的需求修改数据和标题。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](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)