matplot绘制多个子图
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)# 子图1ax1 = 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()# 子图2ax2 = 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()# 子图3ax3 = 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()
浙公网安备 33010602011771号