matplotlib&条形图

23 篇文章 0 订阅
5 篇文章 0 订阅
实验目的

了解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)

输入参数:

  1. left:标量序列,是X坐标轴数据,即每个块的x轴起始位置
  2. height:标量或者标量序列,是Y坐标轴的数据,即每个块的y轴高度
  3. width:标量或者数组,可选参数。默认为:0.8,每一个块的显示宽度
  4. bottom::标量或者数组,可选参数,默认值为None,条形图y坐标即每一个块的底部高度
  5. color:标量或者数组,可选参数,条形图前景色
  6. edgecolor:标量或者数组,可选参数。条形图边界颜色。
  7. linewidth:标量或者数组,可选参数。条形图边界宽度。 如果为None,使用默认linewidth;如果为0,不画边界。默认为None。
  8. xerr=1:标量或者数组,可选参数,x轴将把生成的errorbars用在条形图上,默认为None。
  9. yerr=1:标量或者数组,可选参数。如果不是None,y轴将把生成的errorbars用在条形图上,默认为None。
  10. ecolor:标量或者数组,可选参数,误差bar的颜色,默认为None。
  11. capsize:标量,可选参数。误差bar的线条宽度,默认为None,从errorbar.capsize rcParam获取到值。
  12. orientation=‘vertical’:设置条形图方向。 (horizontal,vertical)
  13. align=“center”:块的位置 (center, left, right)
  14. hold=None

返回值:

bars:matplotlib.container.BarContainer。带有所有bar与errorbar的容器。

(2)函数原型:matplotlib.pyplot.barh(bottom, width, height=0.8, left=None, hold=None, **kwargs)

输入参数:

  1. bottom:标量或者数组。条形图的y坐标。
  2. width:标量或者数组。条形图宽度。
  3. height:标量序列,可选参数,默认值为:0.8。条形图的高度。
  4. 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.在屏幕上打印出下面的图。

在这里插入图片描述

注意:

代码因复制粘贴原因格式可能会有些错误,请自行检查食用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

余生羁绊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值