Barh函数--Matplotlib

函数Barh()–用于绘制条形图
本节的条形图绘制所用到的参数与上一节的柱形图的参数使用一致,本次不再做详细介绍,需要请查看bar函数链接: bar函数–绘制柱形图
Barh函数功能: Make a horizontal bar plot
                            绘制水平条形图

The bars are positioned at y with the given alignment. Their dimensions are given by width and height. The horizontal baseline is left (default 0).Many parameters can take either a single value applying to all bars or a sequence of values, one for each bar.
这些条块Bar以给定的对齐方式放在y处。他们的大小通过指定的宽度和高度确定。水平基线在左侧(默认0)。许多参数可以采用两种方式赋值:一是对所有条块赋予同一个值;二是传入一系列值,每个条块一个单独的值。

函数语法:
barh(y, width, height=0.8, left=None, *, align=‘center’, **kwargs)

函数参数:
y: float or array-like.The y coordinates of the bars. See also align for the alignment of the bars to the coordinates.
浮点型或类数组对象;条形的y轴坐标。另请参见‘align’参数了解柱与坐标的对齐方式

width: float or array-like.The width(s) of the bars.
浮点型或类数组对象;条形的宽度

height: float or array-like, default: 0.8.The heights of the bars.
浮点型或类数组对象;条形的高度,默认值0.8

left: float or array-like, default: 0.The x coordinates of the left sides of the bars.
浮点型或类数组对象;条块左侧的x坐标。

align:{‘center’, ‘edge’}, default: ‘center’.Alignment of the base to the y coordinates*:
对齐方式: 中间,边缘 ,默认为中间。条块底部与y轴坐标的对齐方式
‘center’: Center the bars on the y positions.
‘edge’: Align the bottom edges of the bars with the y positions.
To align the bars on the top edge pass a negative height and align=‘edge’.
中间:y坐标在条块左侧的中间位置
边缘:y坐标在条块左侧的底部位置
设置y坐标在条块左侧的顶部位置需要传入高度为负值同时对其方式为边缘

Other Parameters:
color: color or list of color, optional。The colors of the bar faces.
可选参数,颜色或颜色列表 。条块颜色
edgecolor: color or list of color, optional。The colors of the bar edges.
可选参数:颜色或颜色列表 。条块边缘的颜色
linewidth: float or array-like, optional。Width of the bar edge(s). If 0, don’t draw edges.
线条宽度:可选参数,浮点型或类数组 。条块边缘线的宽度,若值为0,则不绘制边线
tick_label: str or list of str, optional 。The tick labels of the bars. Default: None (Use default numeric labels.)
可选参数,字符串或字符串列表,条块的刻度线标签,默认:使用数字标签
**kwargs: Rectangle properties
其他关键参数: 定义矩形柱子的属性
在这里插入图片描述
绘制条形图:
在这里插入图片描述
使用align参数设置y轴坐标在条块左侧的底部
在这里插入图片描述
设置align在条块左侧上部时,需要同时设置height参数为负值且align为edge
在这里插入图片描述
设置参数left条块儿向右平移
参数 l e f t left left默认值
在这里插入图片描述

参数 l e f t = 3 left=3 left=3,条块儿向右平移距离3
在这里插入图片描述

参数 l e f t left left为条块儿个数相等的数组,每个条块儿向右移动相应的距离
在这里插入图片描述

代码实现:

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False

x = [3, 2, 3, 4, 5, 6, 7, 8]
y = [3, 1, 4, 5, 8, 9, 7, 2]

plt.barh(x, y, tick_label=['q', 'a', 'c', 'e', 'r', 'j', 'b', 'p'],
         color=['brown'], align='center', hatch='/')

plt.title('条形图')

plt.xlabel('箱子重量(Kg)')
plt.ylabel('箱子编号')

plt.show()

在这里插入图片描述

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值