我们使用subplot可以实现在同一个figure对象下绘制多个图像:figure对象:
image.png
每一个这样的图都包含唯一一个figure对象,而一个figure对象里面可以包含多个axes对象,而每一个axes对象又可以绘制多个数据集的图像,在这幅图里,我们在一个figure对象下面使用了一个axes对象并在该axes对象下面绘制了3个数据集的图像。
而subplot可以帮助我们把这三个数据集的图象分开放在不同的坐标轴中(也就是放置在不同的axes对象中)。
数据集
我们首先将数据集实线数据绘制到一个figure对象中, 将虚线数据绘制到另一个figure对象中,这样最后我们的结果就会有两个figure对象绘制而成的图依次弹出来:
image.png
两个figure绘制的源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import pandas as pd from matplotlib import pyplot as plt ''' # one figure, one axes, three drawing objects plt.style.use('seaborn') data = pd.read_csv('data_12.csv') ages = data['Age'] dev_salaries = data['All_Devs'] py_salaries = data['Python'] js_salaries = data['JavaScript'] plt.plot(ages, py_salaries, label='Python') plt.plot(ages, js_salaries, label='JavaScript') plt.plot(ages, dev_salaries, color='#444444', linestyle='--', label='All Devs') plt.legend() plt.title('Median Salary (USD) by Age') plt.xlabel('Ages') plt.ylabel('Median Salary (USD)') plt.tight_layout() plt.show() ''' # two figure each has one axes, one have two drawing obg while another has one. plt.style.use('seaborn') data = pd.read_csv('data_12.csv') ages = data['Age'] dev_salaries = data['All_Devs'] py_salaries = data['Python'] js_salaries = data['JavaScript'] # 创建两个子绘图对象, 每个对象分别有一个figure和axes fig1, ax1 = plt.subplots() fig2, ax2 = plt.subplots() # 在其中一个figure对象中绘制虚线的数据 ax1.plot(ages, dev_salaries, color='#444444', linestyle='--', label='All Devs') # 在另外的figure对象中绘制实线的数据 ax2.plot(ages, py_salaries, label='Python') ax2.plot(ages, js_salaries, label='JavaScript') ax1.legend() ax1.set_title('Median Salary (USD) by Age') ax1.set_ylabel('Median Salary (USD)') ax2.legend() ax2.set_xlabel('Ages') ax2.set_ylabel('Median Salary (USD)') plt.tight_layout() plt.show() # 同时自动保存下两个figure对象的绘制结果 # fig1.savefig('fig1.png') # fig2.savefig('fig2.png') |
我们再尝试将数据绘制到同一个figure中,用两个axes对象分别绘制, 这两个axes对象可以并排,同列......等等, 完全取决于你的参数:
放在同行:
image.png
放在同列:
image.png
详细代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # one figure, two axes, one have two drawing obg while another has one. plt.style.use('seaborn') data = pd.read_csv('data_12.csv') ages = data['Age'] dev_salaries = data['All_Devs'] py_salaries = data['Python'] js_salaries = data['JavaScript'] # 创建一个子绘图对象, 有两个axes对象, 分别放置在1行1列和1行2列, 表现出来的效果就是在同一行上并排绘制两个图 # fig1, (ax1, ax2) = plt.subplots(nrows=1, ncols=2) # 创建一个子绘图对象, 有两个axes对象, 分别放置在1行1列和2行1列, 表现出来的效果就是在同一列上并行绘制两个图 fig1, (ax1, ax2) = plt.subplots(nrows=2, ncols=1) # 在其中一个figure对象中绘制虚线的数据 ax1.plot(ages, dev_salaries, color='#444444', linestyle='--', label='All Devs') # 在另外的figure对象中绘制实线的数据 ax2.plot(ages, py_salaries, label='Python') ax2.plot(ages, js_salaries, label='JavaScript') ax1.legend() ax1.set_title('Median Salary (USD) by Age') ax1.set_ylabel('Median Salary (USD)') ax2.legend() ax2.set_xlabel('Ages') ax2.set_ylabel('Median Salary (USD)') plt.tight_layout() plt.show() |