plt.bar绘图规则——以绘制直方图为例

9 篇文章 0 订阅
8 篇文章 0 订阅

1.bar()的基础参数如下:

x:柱子在x轴上的坐标。
height:柱子的高度,即y轴上的坐标。
width:柱子的宽度。默认值为0.8。
bottom:柱子的基准高度。
align:柱子在x轴上的对齐方式。取值范围为{‘center’, ‘edge’},默认为’center’。
‘center’:x为柱子中端点位置。align=‘center’。
‘edge’:x为柱子左端点位置。如果想让x位于柱子右侧,需要同时设置(负width 以及align=‘edge’)。
eg:
2.实验代码
在这里插入图片描述

import matplotlib.pyplot as plt
import os
x1=[0,1,2,3,4]
n1=[1,2,3,4,5]

bins_width=1
x2=[]
for i in x1:
    x2.append(i+bins_width/2)
    
x3=[]
for i in x1:
    x3.append(i+bins_width)

#plt.rcParams['font.sans-serif']=['SimHei']
#plt.rcParams['axes.unicode_minus'] = False    
    
plt.figure(figsize=(16,4))
plt.subplot(1,2,1)
plt.title('the rules of using plt.bar fuction to draw histogram')
plt.bar(x1,n1,width=1,align='center',label="align='center'")
plt.bar(x1,n1,width=1,align='edge',color='yellow',label="align='edge'")
plt.legend()


plt.subplot(1,2,2)
plt.title('the  examples of using plt.bar subfuction to draw histogram')
plt.bar(x1,n1,width=1,align='edge',color='yellow',label='x为柱子左端点位置(x)')
plt.bar(x2,n1,width=1,align='center',label='x为柱子中端点位置(x+bins_width/2)')
plt.bar(x3,n1,width=-1,align='edge',label='x为柱子右端点位置(x+bins_width)')
plt.bar(x1,n1,width=1,align='edge',color='yellow',label='x为柱子左端点位置')
plt.legend()
plt.show()

参考文章
1.matplotlib之pyplot模块之柱状图(bar():基础参数、外观参数)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值