P70思考与练习1

1.叙述Pandas和Matplotlib绘图工具之间的关系。如何在绘图中综合使用两种工具的绘图函数,达到既快速绘图又可精细化设置图元的目标。

1.pandas封装了Matplotlib的主要绘图功能。pandas基于Series与DataFrame绘图,使用Series与DataFrame封装数据,调Series.plot()或DataFrame.plot()完成绘图。pandas绘图简单直接,若要更细致地控制图标样式,如添加标注、在一幅图中包含多副子图,必须使用Matplotlib提供的基础函数。Matplotlib中使用pyplot的图元设置函数来实现图形的精细化设置,能够对pandas绘出的图形进行精细化设置。
我们可以先将数据封装成Series或DataFrame,通过Pandas快速绘图,最后再通过Matplotlib中的pyplot图元设置函数对图形进一步精细化设置,达到精细化绘图的目的。

 2. 2012~2020年我国人均可支配收入为[1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21](单位:万元)。按照要求绘制以下图形。

1)模仿例4-1和4-3,绘制人均可支配收入折线图。用小矩形标记数据点,黑色虚线,用注解标注最高点,图例标题“Income ”,设置坐标轴标题,最后将图形保存为jpg文件。

2)模仿例4-2,使用多个子图分别绘制人均可支配收入的折线图、箱形图以及柱状图(效果如下图所示)。

【提示】:

(1)本实验准备数据时可使用Series对象或DataFrame对象。

(2)创建3个子图分别使用(2,2,1)、(2,2,2)和(2,1,2)作为参数。

(3)使用plt.subplots_adjust()函数调整子图间距离,以便添加图标题。

 新书第二版要求的画图效果

第(1)题:

共列举以下三种方法

【方法一】:使用Series.plot画图,数据为Series形式

【方法二】:使用DataFrame.plot画图,数据为DataFrame形式

【方法三】:使用plt.plot函数画图(默认kind='line‘,画折线图),数据为列表形式

#方法一:Series.plot
import matplotlib.pyplot as plt
from pandas import Series
Income_data = Series([1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21],index = ['2012','2013','2014','2015','2016','2017','2018','2019','2020'])
Income_data.plot(title = '2012-2020 年人均可支配收入',marker = 's',linestyle = 'dotted',grid = True,label = 'Income',
                 yticks = [0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5],c = 'black')     #label代表图例,c代表线、点的颜色
plt.xlabel('Year')                              #添加x轴标题
plt.ylabel('Income(RMB Ten Thousand)')          #添加y轴标题
plt.legend()                                    #显示图例,若无则不显示
plt.annotate('Largest!',xy = (8,3.21),xytext = (6.1,2.6),arrowprops = dict(arrowstyle = '->',color = 'r'),color = 'r')     #xy为箭头位置坐标,xytext为文字起点位置
plt.show()

#方法二:DataFrame.plot
import matplotlib.pyplot as plt
from pandas import DataFrame
#DataFrame1中columns即为图例,index即为横坐标值
Income_data = DataFrame([1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21],index = ['2012','2013','2014','2015','2016','2017','2018','2019','2020'],columns = ['Income'])
Income_data.plot(title = '2012-2020 年人均可支配收入',marker = 's',linestyle = 'dotted',grid = True,
                 yticks = [0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5],c = 'black')     
plt.xlabel('Year')                              
plt.ylabel('Income(RMB Ten Thousand)')          
plt.annotate('Largest!',xy = (8,3.21),xytext = (6.1,2.6),arrowprops = dict(arrowstyle = '->',color = 'r'),color = 'r')     
plt.show()

#方法三:plt.plot
import matplotlib.pyplot as plt
Income_data = [1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21]
plt.plot(['2012','2013','2014','2015','2016','2017','2018','2019','2020'],[1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21],marker = 's',
         linestyle = 'dotted',c = 'black',label = 'Income')
plt.title('2012-2020 年人均可支配收入')
plt.yticks([0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5])
plt.xlim(0,8)             #设置x轴刻度范围
plt.xlabel('Year')                              
plt.ylabel('Income(RMB Ten Thousand)')
plt.annotate('Largest!',xy = (8,3.21),xytext = (6.1,2.6),arrowprops = dict(arrowstyle = '->',color = 'r'),color = 'r')     
plt.legend()
plt.grid()                #显示网格线

结果如下:

第(2)题:

共列举以下6种方法

可采用figure.add_subplot()函数或plt.subplot()两种函数创建子图。而数据形式分别为Series、DataFrame与列表三种形式。由此列举6种方法求解。

前三种方法采用figure.add_subplot()函数

【方法一】:采用figure.add_subplot()函数创建子图。使用Series.plot画图,数据为Series形式。

【方法二】:采用figure.add_subplot()函数创建子图。使用DataFrame.plot画图,数据为DataFrame形式。

【方法三】:采用figure.add_subplot()函数创建子图。使用各类画图函数画图(plt.plot()画折线图,plt.boxplot()画箱形图,plt.bar()画柱状图),数据为列表形式。

后三种方法采用plt.subplot()函数

【方法四】:采用plt.subplot()函数创建子图。使用Series.plot绘图,数据为Series形式。

【方法五】:采用plt.subplot()函数创建子图。使用DataFrame.plot画图,数据为DataFrame形式。

【方法六】:采用plt.subplot()函数创建子图。使用各类画图函数画图(plt.plot()画折线图,plt.boxplot()画箱形图,plt.bar()画柱状图),数据为列表形式。

前三种方法(采用figure.add_subplot()函数):

先定义画布大小:fig = plt.figure(figsize = ()) 

再创建子图:fig.add_subplot()或者ax1... = fig.add_subplot()

对于Series与列表类型的数据:可不返回ax=ax1,ax=ax2,ax=ax3....。(也可返回)

对于DataFrame类型的数据:写为ax1(ax2、ax3...)  = fig.add_subplot(),每次画图在画图函数里都要返回ax=ax1,ax=ax2,ax=ax3....。例如DataFrame.plot(...,ax = ax1)等,否则画图出错。

#方法一:采用figure.add_subplot()函数创建子图。使用Series.plot画图,数据为Series形式
import matplotlib.pyplot as plt
from pandas import Series
Income_data =Series([1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21],index = ['2012','2013','2014','2015','2016','2017','2018','2019','2020'])
fig = plt.figure(figsize = (10,6))    #figure创建绘图对象,figsize设置画布尺寸
#将绘图区域分为2行2列4份,在第1份上作图
ax1 = fig.add_subplot(2,2,1)
#绘制折线图
ax1.plot(Income_data)                #采用AxesSubplot绘制折线图 
plt.title('Line chart')
plt.xlabel('Year')
plt.ylabel('Incone')
'''
还可采用Series.plot()函数绘制折线图:
Income_data.plot(title = 'Line chart')                                                     #采用Series.plot画折线图
plt.xticks(range(0,9),['2012','2013','2014','2015','2016','2017','2018','2019','2020'])    #让x轴刻度显示全
plt.xlim(-0.5,8.5)                                                                         #增大x轴刻度范围
plt.xlabel('Year')
plt.ylabel('Income')
'''
#将绘图区域分为2行2列4份,在第2份上作图
ax2 = fig.add_subplot(2,2,2)         #也可去掉"ax2="写为fig.add_subplot(2,2,2)
#绘制箱形图
Income_data.plot(kind = 'box',title = 'Box-whisker plot')
plt.xlabel('2012-2020')
plt.ylabel('Income')
plt.xticks([])
#将绘图区域分为2行1列2份,在第2份作图
ax3 = fig.add_subplot(2,1,2)         #也可去掉"ax3="写为fig.add_subplot(2,1,2)
#绘制柱状图
Income_data.plot(kind = 'bar',label = 'Income')
plt.title('Bar chart')
plt.xlabel('Year')
plt.ylabel('Income')
plt.legend()                         #显示图例
#调整子图间距离
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)


#方法二:采用figure.add_subplot()函数创建子图。使用DataFrame.plot画图,数据为DataFrame形式,需返回ax=ax1,ax=ax2,ax=ax3
import matplotlib.pyplot as plt
from pandas import DataFrame
Income_data =DataFrame([1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21],index = ['2012','2013','2014','2015','2016','2017','2018','2019','2020'],
                      columns = ['Income'])
fig = plt.figure(figsize = (10,6))
#画折线图
ax1 = fig.add_subplot(221)       #(2,2,1)可连写(221)
Income_data.plot(title = 'Line chart',legend = False,ax = ax1)                  #返回ax1                       #legend = False去掉图例
plt.xticks(range(0,9),['2012','2013','2014','2015','2016','2017','2018','2019','2020'])     #让x轴刻度显示全
plt.xlim(-0.5,8.5)               #增大x轴刻度范围
plt.xlabel('Year')
plt.ylabel('Income')
#画箱形图
ax2 = fig.add_subplot(222)
Income_data.plot(kind = 'box',title = 'Box-whisker plot',xticks = [],ax = ax2)  #返回ax2
plt.xlabel('2012-2020')
plt.ylabel('Income')
#画柱状图
ax3 = fig.add_subplot(212)
Income_data.plot(kind = 'bar',title = 'Bar chart',ax = ax3)                     #返回ax3
plt.xlabel('Year')
plt.ylabel('Income')
#调间距
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)
plt.savefig('Income222.jpg',dpi = 400,bbox_inches = 'tight')             #保存导出图像
plt.show()


#方法三:采用figure.add_subplot()函数创建子图。使用各类画图函数画图,数据为列表形式
import matplotlib.pyplot as plt
Income_data = [1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21]
fig = plt.figure(figsize = (10,6))
#画折线图
fig.add_subplot(221)    
plt.plot(['2012','2013','2014','2015','2016','2017','2018','2019','2020'],Income_data)        #折线图函数:plt.plot()
plt.title('Line chart')
plt.xlabel('Year')
plt.ylabel('Incone')
#画箱形图
fig.add_subplot(222)
plt.boxplot(Income_data,boxprops = {'color':'#1F77B4'},medianprops = {'color':'green'},whiskerprops={'color':'#1F77B4'})     
#箱形图函数:plt.boxplot()。boxprops设置箱体的属性,whiskerprops设置须的属性,medianprops设置中位数的属性
plt.title('Box-whisker plot')
plt.xlabel('2012-2020')
plt.xticks([])             #不显示坐标轴刻度
plt.ylabel('Income')
#画柱状图
fig.add_subplot(212)
plt.bar(['2012','2013','2014','2015','2016','2017','2018','2019','2020'],Income_data,label = 'Income')   #柱状图函数:plt.bar(x,height)
plt.title('Bar chart')
plt.legend()
plt.xlabel('Year')
plt.xticks(rotation = 90)  #rotation设置刻度旋转角度值
plt.ylabel('Income')    
#调间距
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)
plt.savefig('Income2.jpg',dpi = 400,bbox_inches = 'tight')
plt.show()

后三种方法(采用plt.subplot()函数):

思路同上三种方法。

先定义画布大小:plt.figure(figsize = ()) 

再创建子图:plt.subplot()

对于Series与列表类型的数据:可不返回ax=ax1,ax=ax2,ax=ax3....。(也可返回)

对于DataFrame类型的数据:写为ax1(ax2、ax3...)  = plt.subplot(),每次画图在画图函数里都要返回ax=ax1,ax=ax2,ax=ax3....。否则画图出错。如下:

#方法四:采用plt.subplot()函数创建子图。使用Series.plot绘图,数据为Series形式
import matplotlib.pyplot as plt
from pandas import Series
Income_data = Series([1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21],index = ['2012','2013','2014','2015','2016','2017','2018','2019','2020'])
plt.figure(figsize = (10,6))
#画折线图
plt.subplot(221) 
Income_data.plot(title = 'Line chart')                                                  #用Series.plot画折线图 
plt.xticks(range(0,9),['2012','2013','2014','2015','2016','2017','2018','2019','2020'])    #让x轴刻度显示全
plt.xlim(-0.5,8.5)                                                                         #增大x轴刻度范围
plt.xlabel('Year')
plt.ylabel('Income')
#画箱形图
plt.subplot(222)
Income_data.plot(kind = 'box',title = 'Box-whisker plot')
plt.xlabel('2012-2020')
plt.ylabel('Income')
plt.xticks([])
#画柱状图
plt.subplot(212)
Income_data.plot(kind = 'bar',label = 'Income')
plt.title('Bar chart')
plt.xlabel('Year')
plt.ylabel('Income')
plt.legend()    
#调整子图间距离
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)


#方法五:采用plt.subplot()函数创建子图。使用DataFrame.plot画图,数据为DataFrame形式,需返回ax=ax1,ax=ax2,ax=ax3
import matplotlib.pyplot as plt
from pandas import DataFrame
Income_data =DataFrame([1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21],index = ['2012','2013','2014','2015','2016','2017','2018','2019','2020'],
                      columns = ['Income'])
plt.figure(figsize = (8,6))
#画折线图
ax1 = plt.subplot(221)
Income_data.plot(title = 'Line chart',legend = False,ax = ax1)                             #legend = False去掉图例
plt.xticks(range(0,9),['2012','2013','2014','2015','2016','2017','2018','2019','2020'])     #让x轴刻度显示全
plt.xlim(-0.5,8.5)               #增大x轴刻度范围
plt.xlabel('Year')
plt.ylabel('Income')
#画箱形图
ax2 = plt.subplot(222)
Income_data.plot(kind = 'box',title = 'Box-whisker plot',xticks = [],ax = ax2)
plt.xlabel('2012-2020')
plt.ylabel('Income')
#画柱状图
ax3 = plt.subplot(212)
Income_data.plot(kind = 'bar',title = 'Bar chart',ax = ax3)
plt.xlabel('Year')
plt.ylabel('Income')
#调间距
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)


#方法六:采用plt.subplot()函数创建子图。使用各类画图函数画图,数据为列表形式
import matplotlib.pyplot as plt
Income_data = [1.47, 1.62, 1.78, 1.94, 2.38, 2.60,2.82,3.07,3.21]
plt.figure(figsize = (10,6))
#画折线图
plt.subplot(221)
plt.plot(['2012','2013','2014','2015','2016','2017','2018','2019','2020'],Income_data)                  #折线图函数:plt.plot()
plt.title('Line chart')
plt.xlabel('Year')
plt.ylabel('Incone')
#画箱形图
plt.subplot(222)
plt.boxplot(Income_data,boxprops = {'color':'#1F77B4'},medianprops = {'color':'green'},whiskerprops={'color':'#1F77B4'})     #箱形图函数:plt.boxplot()
plt.title('Box-whisker plot')
plt.xlabel('2012-2020')
plt.xticks([])               #不显示坐标轴刻度,也可写为plt.xticks(())
plt.ylabel('Income')
#画柱状图
plt.subplot(212)
plt.bar(['2012','2013','2014','2015','2016','2017','2018','2019','2020'],Income_data,label = 'Income')   #柱状图函数:plt.bar(x,height)
plt.title('Bar chart')
plt.legend()
plt.xlabel('Year')
plt.xticks(rotation = 90)    #rotation设置刻度旋转角度值
plt.ylabel('Income')    
#调间距
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)

六种方法运行结果皆如下:

 【老书第一版此题】

宋晖《数据科学技术与应用》第一版老书中,该题目为:

2012~2017年我国人均可支配收入为[1.47, 1.62, 1.78, 1.94, 2.38, 2.60](单位:万元)。

题目要求图形如下:

 

老书第一版要求的画图效果

第(1)题:

同样有类似的三种方法:

#方法一:采用Series.plot绘图。
import matplotlib.pyplot as plt
from pandas import Series
plt.figure()
Income_data = Series([1.47,1.62,1.78,1.94,2.38,2.60],index = ['2012','2013','2014','2015','2016','2017'])
Income_data.plot(title = 'Income chart',marker = 's',color = 'black',linestyle = 'dashed',grid = True,
                 yticks = [0.0,0.5,1.0,1.5,2.0,2.5,3.0],use_index = True)   #若不加use_index = True,则x轴刻度范围会多一些
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income(RMB Ten Thousand)',fontsize = 12)
plt.legend(('Income',))     #添加逗号让图例文本显示全
plt.annotate('Largest!',color = 'red',xy = (5,2.60),xytext = (3,2.55),arrowprops = dict(arrowstyle = '->',color = 'red'))
plt.savefig('Income chart.jpg',dpi = 400,bbox_inches = 'tight')   #保存为jpg图片
plt.show()


#方法二:采用DataFrame.plot绘图,用columns做标签,无需设label。同时x,y轴刻度采用函数取得
import matplotlib.pyplot as plt
import numpy as np
from pandas import DataFrame
%matplotlib inline
Income = [1.47,1.62,1.78,1.94,2.38,2.60]
data = DataFrame({'Income':Income},index = np.arange(2012,2018))#此处需要注意index中数的坐标,(标注释时xy中坐标改变)
data.plot(title = 'Income chart',linestyle = 'dashed',marker = 's',color = 'k',grid = True,yticks = np.arange(0.0,3.5,0.5))  #此处可以显示或者不显示图例,加legend  = True/False
#精细设置图元
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income(RMB Ten Thousand)',fontsize = 12)
plt.annotate('Largest!',color = 'r',xy = (2017,2.60),xytext = (2015,2.55),arrowprops = dict(arrowstyle = '->',color = 'r'))    
plt.savefig('Income chart.jpg',dpi = 400,bbox_inches = 'tight')
plt.show()


#方法三:采用plt.plot画图,设置坐标轴与标题的字体
import matplotlib.pyplot as plt
import numpy as np
Income = [1.47,1.62,1.78,1.94,2.38,2.60]
plt.plot(Income,linestyle = 'dashed',color = 'k',marker = 's',label = 'Income')     #注:里面无grid,需要直接设置
plt.xticks(range(0,6),['2012','2013','2014','2015','2016','2017'])                  #将x轴刻度映射为字符串
plt.yticks(np.arange(0,3.5,0.5))
plt.title('Income chart',fontdict = {'family':'Times New Roman','size':12})
plt.xlabel('Year',fontdict = {'family':'Times New Roman','size':12})
plt.ylabel('Income(RMB Ten Thousand)',fontdict = {'family':'Times New Roman','size':12})
plt.xlim(0,5)
plt.legend(loc = 1)       #loc = 1表示图例位于第一象限。#此处亦可写plt.legend(('Income',),loc = 'upper right'),不用label #亦可写plt.legend()
plt.grid()
plt.annotate('Largest!',xy = (5,2.60),xytext = (3,2.55),arrowprops = dict(arrowstyle = '->',color = 'r'),color = 'red')   
plt.savefig('Income chart.jpg',dpi = 400,bbox_inches = 'tight')
plt.show()

结果如下:

 第(2)题:

同样列举以下6种方法:
【方法一】:fig.add_subplot绘图,Series.plot()绘图,数据为series形式
【方法二】:fig.add_subplot绘图,DataFrame.plot()绘图,数据为DataFrame形式
【方法三】:fig.add_subplot()函数,运用各类画图函数画图,数据为列表形式
【方法四】:采用subplot()函数绘图。Series.plot()函数,数据为Series形式
【方法五】:采用subplot()函数绘图。DataFrame.plot()函数,数据为DataFrame形式
【方法六】:采用subplot()函数,运用各类画图函数画图,数据为列表形式

六种方法如下:

#方法一:fig.add_subplot绘图,Series.plot()绘图,数据为series形式
import matplotlib.pyplot as plt
from pandas import Series
data = Series([1.47,1.62,1.78,1.94,2.38,2.60],index = ['2012','2013','2014','2015','2016','2017'])
#定义图形大小
fig = plt.figure(figsize = (6,6))
#绘制折线图
ax1 = fig.add_subplot(2,2,1)
ax1.plot(data)
plt.title('line chart')
plt.xticks(range(0,6,2),['2012','2014','2016'])
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#绘制箱形图
ax2 = fig.add_subplot(2,2,2)
data.plot(kind = 'box',xticks = [])     #xticks = []表示无刻度
plt.title('box-whisker plot')
plt.xlabel('2012—2017',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#绘制柱状图
ax3 = fig.add_subplot(2,1,2)
data.plot(kind = 'bar')
plt.legend(('Income',))
plt.title('bar chart')
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#调整子图间距离
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)       #wspace、hspace分别表示子图之间左右、上下的间距
plt.savefig('Image2.jpg',dpi = 400,bbox_inches = 'tight')
plt.show()


#方法二:fig.add_subplot绘图,DataFrame.plot()绘图,数据为DataFrame形式
import matplotlib.pyplot as plt
from pandas import DataFrame
data1 = [1.47,1.62,1.78,1.94,2.38,2.60]
Incomedata = DataFrame({'Income':data1},index = ['2012','2013','2014','2015','2016','2017'])
fig = plt.figure(figsize = (6,6))
#绘制折线图
ax1 = fig.add_subplot(2,2,1)
Incomedata.plot(title = 'line chart',legend = False,ax = ax1)   #legend = False去掉标注 。
#另外重要发现:不加ax=ax1画图出错。而Series.plot未发现出错
plt.xticks(range(0,6,2),['2012','2014','2016'])
plt.xlabel('Year',fontsize = 12)
plt.xlim(-0.5,5.5)      #扩大x刻度范围
plt.ylabel('Income',fontsize = 12)
#绘制箱线图
ax2 = fig.add_subplot(2,2,2)
Incomedata.plot(kind = 'box',title = 'box-whisker plot',ax = ax2)
plt.xticks(())
plt.xlabel('2012—2017',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#绘制柱状图
ax3 = fig.add_subplot(2,1,2)
Incomedata.plot(kind = 'bar',title = 'bar chart',ax = ax3)
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#调整子图间间距
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)
plt.show()


#方法三:运用各类画图函数画图:采用fig.add_subplot()函数,数据为列表形式
import matplotlib.pyplot as plt
data = [1.47,1.62,1.78,1.94,2.38,2.60]
fig = plt.figure(figsize = (6,6))
#折线图
ax1 = fig.add_subplot(2,2,1)
plt.plot(data)
plt.title('line chart')
plt.xticks(range(0,6,2),['2012','2014','2016'])
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#箱型图
ax2 = fig.add_subplot(2,2,2)
plt.boxplot(data,boxprops = {'color':'#1F77B4'},medianprops = {'color':'green'},whiskerprops={'color':'#1F77B4'})    
#boxprops设置箱体的属性,whiskerprops设置须的属性,medianprops设置中位数的属性
plt.xticks(())   #双括号不显示坐标轴刻度
plt.title('box-whisker plot')
plt.xlabel('2012—2017',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#柱形图
ax3 = fig.add_subplot(2,1,2)
plt.bar(['2012','2013','2014','2015','2016','2017'],[1.47,1.62,1.78,1.94,2.38,2.60])   #bar(x,height)
plt.legend(('Income',))
plt.xticks(rotation = 90)    #x刻度倾斜90度
plt.title('bar chart')
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#调整子图间距离
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)   


#方法四:采用subplot()函数绘图。Series.plot()函数,数据为Series形式
import matplotlib.pyplot as plt
from pandas import Series
plt.figure(figsize = (6,6))
#绘制折线图
plt.subplot(2,2,1)
data = Series([1.47,1.62,1.78,1.94,2.38,2.60],index = ['2012','2013','2014','2015','2016','2017'])
plt.plot(data)
plt.title('line chart')
plt.xticks(range(0,6,2),['2012','2014','2016'])
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#绘制箱型图
plt.subplot(2,2,2)
data.plot(title = 'box-whisker plot',kind = 'box',xticks = [])
plt.xlabel('2012—2017',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#绘制柱形图
plt.subplot(2,1,2)
data.plot(kind = 'bar')
plt.legend(('Income',))
plt.title('bar chart')
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#调整子图间距离
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)   
plt.show()


#方法五:采用subplot()函数绘图。DataFrame.plot()函数,数据为DataFrame形式
import matplotlib.pyplot as plt
from pandas import DataFrame
data1 = [1.47,1.62,1.78,1.94,2.38,2.60]
Incomedata = DataFrame({'Income':data1},index = ['2012','2013','2014','2015','2016','2017'])
plt.figure(figsize = (6,6))
#折线图
ax1 = plt.subplot(221)    #将第一个画板划分为2行1列组成的区块,并获取到第一块区域
Incomedata.plot(title = 'line chart',legend = False,ax = ax1)  #若无ax = ax1,则画不出图
plt.xticks(range(0,6,2),['2012','2014','2016'])
plt.xlabel('Year',fontsize = 12)
plt.xlim(-0.5,5.5)      #扩大x刻度范围
plt.ylabel('Income',fontsize = 12)
#绘制箱线图
ax2 = plt.subplot(222)
Incomedata.plot(kind = 'box',title = 'box-whisker plot',ax = ax2)
plt.xticks(())
plt.xlabel('2012—2017',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#绘制柱状图
ax3 = plt.subplot(212)
Incomedata.plot(kind = 'bar',title = 'bar chart',ax = ax3)
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#调整子图间间距
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)


#方法六:运用各类画图函数画图:采用subplot()函数,数据为列表形式
import matplotlib.pyplot as plt
data = [1.47,1.62,1.78,1.94,2.38,2.60]
fig = plt.figure(figsize = (6,6))
#折线图
plt.subplot(2,2,1)
plt.plot(data)
plt.title('line chart')
plt.xticks(range(0,6,2),['2012','2014','2016'])
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#箱型图
plt.subplot(2,2,2)
plt.boxplot(data,boxprops = {'color':'#1F77B4'},medianprops = {'color':'green'},whiskerprops={'color':'#1F77B4'})    
#boxprops设置箱体的属性,whiskerprops设置须的属性,medianprops设置中位数的属性
plt.xticks(())   #双括号不显示坐标轴刻度
plt.title('box-whisker plot')
plt.xlabel('2012—2017',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#柱形图
plt.subplot(2,1,2)
plt.bar(['2012','2013','2014','2015','2016','2017'],[1.47,1.62,1.78,1.94,2.38,2.60])   #bar(x,height)
plt.legend(('Income',))
plt.xticks(rotation = 90)    #x刻度倾斜90度
plt.title('bar chart')
plt.xlabel('Year',fontsize = 12)
plt.ylabel('Income',fontsize = 12)
#调整子图间距离
plt.subplots_adjust(wspace = 0.5,hspace = 0.5)   

运行结果如下:

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐