matplot绘制多个子图

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#matplotlib inline
# 画第1个图:折线图
x=np.arange(1,100)
plt.subplot(221)
plt.plot(x,x*x)
# 画第2个图:散点图
plt.subplot(222)
plt.scatter(np.arange(0,10), np.random.rand(10))
# 画第3个图:饼图
plt.subplot(223)
plt.pie(x=[15,30,45,10],labels=list('ABCD'),autopct='%.0f',explode=[0,0.05,0,0])
# 画第4个图:条形图
plt.subplot(224)
plt.bar([20,10,30,25,15],[25,15,35,30,20],color='b') 
plt.show()
 
 
注意:一行两列的子图设置格式为:
ax = plt.subplot(121)
ax = plt.subplot(122)
 
 
**************双子图共享Y轴  **************

import matplotlib.pyplot as plt
import numpy as np


# First create some toy data:
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)

# Create two subplots and unpack the output array immediately
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)


**************双子图共享X轴  **************

# Share a X axis with each column of subplots

plt.subplots(2, 2, sharex='col')

 

**************多子图共享Y轴  **************

# Share a Y axis with each row of subplots
plt.subplots(2, 2, sharey='row')

**************多子图共享X轴  **************

# Share a X axis with each column of subplots
plt.subplots(2, 2, sharex='col')

 

**************多子图共享X-Y轴  **************

# Share both X and Y axes with all subplots
plt.subplots(2, 2, sharex='all', sharey='all')

# Note that this is the same as
plt.subplots(2, 2, sharex=True, sharey=True)

 

 

另一种绘制多子图的方法:

# 创建画布

fig = plt.figure(figsize = (30,10), dpi = 80)
# 子图1
ax1 = plt.subplot(131)
ax1.set_title('Open Price')
ax1.plot(testing_set.values[:,0], color = 'red', label = 'Real Open Price')
ax1.plot(predicted_stock_price[:,0], color = 'blue', label = 'Predicted Open Price')
plt.setp(ax1.get_xticklabels(), fontsize=6)
ax1.legend()
# 子图2
ax2 = plt.subplot(132,sharey=ax1)
ax2.set_title('High Price')
ax2.plot(testing_set.values[:,1], color = 'red', label = 'Real High Price')
ax2.plot(predicted_stock_price[:,1], color = 'blue', label = 'Predicted High Price')
ax2.legend()
# 子图3
ax3 = plt.subplot(133,sharey=ax1)
ax3.set_title('Low Price')
ax3.plot(testing_set.values[:,2], color = 'red', label = 'Real Low Price')
ax3.plot(predicted_stock_price[:,2], color = 'blue', label = 'Predicted Low Price')
ax3.legend()
plt.show()
 
一种绘制折线阴影的方法:
 

 

 
 
posted @ 2022-02-27 16:34  呦呦南山  阅读(647)  评论(0编辑  收藏  举报