实验目的
了解bar函数的每个参数的含义
掌握使用matplotlib画条形图的方法
实验原理
条形图:
(1)函数原型:matplotlib.pyplot.bar(left, height, width=0.8, bottom=None, hold=None, **kwargs)
创建一个水平条形图。创建一个带有矩形边界的水平条,设置如下:
left, left + width, bottom, bottom + height
(left, right, bottom and top edges)
输入参数:
- left:标量序列,是X坐标轴数据,即每个块的x轴起始位置
- height:标量或者标量序列,是Y坐标轴的数据,即每个块的y轴高度
- width:标量或者数组,可选参数。默认为:0.8,每一个块的显示宽度
- bottom::标量或者数组,可选参数,默认值为None,条形图y坐标即每一个块的底部高度
- color:标量或者数组,可选参数,条形图前景色
- edgecolor:标量或者数组,可选参数。条形图边界颜色。
- linewidth:标量或者数组,可选参数。条形图边界宽度。 如果为None,使用默认linewidth;如果为0,不画边界。默认为None。
- xerr=1:标量或者数组,可选参数,x轴将把生成的errorbars用在条形图上,默认为None。
- yerr=1:标量或者数组,可选参数。如果不是None,y轴将把生成的errorbars用在条形图上,默认为None。
- ecolor:标量或者数组,可选参数,误差bar的颜色,默认为None。
- capsize:标量,可选参数。误差bar的线条宽度,默认为None,从errorbar.capsize rcParam获取到值。
- orientation=‘vertical’:设置条形图方向。 (horizontal,vertical)
- align=“center”:块的位置 (center, left, right)
- hold=None
返回值:
bars:matplotlib.container.BarContainer。带有所有bar与errorbar的容器。
(2)函数原型:matplotlib.pyplot.barh(bottom, width, height=0.8, left=None, hold=None, **kwargs)
输入参数:
- bottom:标量或者数组。条形图的y坐标。
- width:标量或者数组。条形图宽度。
- height:标量序列,可选参数,默认值为:0.8。条形图的高度。
- left:标量序列。条形图左边的X坐标
返回值:matplotlib.patches.Rectangle实例。
实验环境
Python 3.6.1
PyCharm
实验内容
使用matplotlib.pyplot中的bar或barh函数绘制条形图。
实验步骤
1.打开Pycharm,选择Create New Project,
创建名为matplotlib7的项目。
2.打开matplotlib7项目,右键选择New=>Python File,
创建名为bar的Python文件。
3.打开bar.py文件,编写代码,用于绘制bar类型的条形图。
导入外包
import numpy as np
import matplotlib.pyplot as plt
4.创建一张图
fig=plt.figure(1)
5.创建一个子图
ax1=plt.subplot(111)
6.使用numpy包中的array函数绘制绘图所需的数据。
data=np.array([15,20,18,25])
7.准备绘制条形图的参数,绘制的条形宽度width=0.5,绘制的条形位置(中心)x_bar=np.arange(4),条形图的高度(height=data)。
width=0.5
x_bar=np.arange(4)
8.通过plt.bar函数来绘制条形的主体,并传入宽度width,位置x_bar,高度data,颜色color="lightblue"等参数。
rect=ax1.bar(left=x_bar,height=data,width=width,color='lightblue')
9.向条形图添加数据标签。
for rec in rect:
x=rec.get_x()
height=rec.get_height()
ax1.text(x+0.1,1.02*height,str(height))
10.绘制x,y坐标轴刻度及标签,以及图形标题。
ax1.set_xticks(x_bar) #x轴刻度
ax1.set_xticklabels(('first','second','third','fourth')) #x轴刻度标签
ax1.set_ylabel('Y Axis') #y轴标签
ax1.set_title("The Bar Graph") #子图的标题
ax1.grid(True) #绘制网格
ax1.set_ylim(0,28) #绘制y轴的刻度范围
plt.show()
11.完整代码如下:
import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure(1)
ax1=plt.subplot(111)
data=np.array([15,20,18,25])
width=0.5
x_bar=np.arange(4)
rect=ax1.bar(left=x_bar,height=data,width=width,color='lightblue')
for rec in rect:
x=rec.get_x()
height=rec.get_height()
ax1.text(x+0.1,1.02*height,str(height))
ax1.set_xticks(x_bar) #x轴刻度
ax1.set_xticklabels(('first','second','third','fourth')) #x轴刻度标签
ax1.set_ylabel('Y Axis') #y轴标签
ax1.set_title("The Bar Graph") #子图的标题
ax1.grid(True) #绘制网格
ax1.set_ylim(0,28) #绘制y轴的刻度范围
plt.show()
12.代码编写完毕,在bar.py文件内,点击右键=》Run ‘bar’,执行bar.py文件。
13.在屏幕上打印出下面的图。
14.创建barh条形图。
打开matplotlib7项目,右键选择New=>Python File,
创建名为barh的Python文件。
15.打开barh.py文件,编写代码,用于绘制barh类型的条形图。
导入外包
import numpy as np
import matplotlib.pyplot as plt
16.创建一张图
fig=plt.figure(1)
17.创建一个子图
ax1=fig.add_subplots(111)
18.使用numpy包中的array函数绘制绘图所需的数据。
data=np.array([30,40,35,50])
19.准备绘制条形图的参数,绘制的条形高度height=0.35,绘制的条形位置(中心)bottom=np.arange(4)。
height=0.35
bottom=np.arange(4)
20.通过plt.barh函数来绘制条形的主体,并传入宽度data,位置bottom,高度height,颜色color="lightbgreen"等参数。
rect=ax1.barh(bottom=bottom,width=data,height=height,color='lightgreen',align='center')
21.向条形图添上数据标签。
for rec in rect:
y=rec.get_y()
width=rec.get_width()
ax1.text(1.02*width,y+0.15,str(width))
22.绘制x,y坐标轴刻度及标签,以及图形标题。
ax1.set_yticks(bottom) #y轴刻度
ax1.set_yticklabels(('first','second','third','fourth')) #y轴刻度标签
ax1.set_ylabel('category') #y轴标签
ax1.set_xlabel('number')
ax1.set_title("The Barh Graph") #子图的标题
ax1.set_xlim(0,53) #绘制x轴的刻度范围
plt.show()
23.完整代码:
import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure(1)
ax1=fig.add_subplot(111)
data=np.array([30,40,35,50])
height=0.35
bottom=np.arange(4)
rect=ax1.barh(bottom=bottom,width=data,height=height,color='lightgreen',align='center')
for rec in rect:
y=rec.get_y()
width=rec.get_width()
ax1.text(1.02*width,y+0.15,str(width))
ax1.set_yticks(bottom) #y轴刻度
ax1.set_yticklabels(('first','second','third','fourth')) #y轴刻度标签
ax1.set_ylabel('category') #y轴标签
ax1.set_xlabel('number')
ax1.set_title("The Barh Graph") #子图的标题
ax1.set_xlim(0,53) #绘制x轴的刻度范围
plt.show()
24.代码编写完毕,在barh.py文件内,点击右键=》Run ‘barh’,执行barh.py文件。
25.在屏幕上打印出下面的图。
注意:
代码因复制粘贴原因格式可能会有些错误,请自行检查食用。