【Matplotlib】matplotlib.pyplot.bar()函数用法

matplotlib.pyplot.bar用法


matplotlib.pyplot.bar

函数原型如下

matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align=‘center’, data=None, **kwargs)

x 表示x坐标,数据类型为int或float类型。
height 表示柱状图的高度,也就是y坐标值,数据类型为int或float类型。
width 表示柱状图的宽度,取值在0~1之间,默认为0.8。
bottom 柱状图的起始位置,也就是y轴的起始坐标。
align 柱状图的对齐方式,默认为’center’(以x坐标为中心),‘edge’:将条形的左边缘与x对齐。

color 柱状图颜色
edgecolor 边框颜色
linewidth 边框宽度
tick_label 下标标签


简单应用:

import matplotlib.pyplot as plt

x = ['apple','banana','pear','watermalon']
y = [40,52,25,33]

#指定显示中文
plt.rcParams['font.sans-serif']=['SimHei']#指定默认字体,不指定中文可能无法正常显示
plt.rcParams['axes.unicode_minus']=False#解决保存图像是负号'-'显示为方块的问题

plt.figure(figsize=(10,7))
plt.bar(range(len(x)),y,tick_label=x,color='blue',edgecolor='white',width=0.5)


plt.xticks(rotation=45,fontsize=15)#调节横坐标倾斜度
plt.yticks(fontsize=15)#纵坐标字号
plt.legend()#图例
plt.title('''水果销量''',fontsize=20)#标题

plt.show()

函数matplotlib.pyplot.bar()是用来绘制垂直条形图的。它的函数签名为matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)。在这个函数中,x表示条形图的位置,height表示每个条形图的高度,width表示条形图的宽度,默认为0.8。bottom表示条形图的基线位置,align表示条形图的对齐方式(默认为'center')。data参数可以传入一个DataFrame或一个类似数组的数据对象。其他的参数可以用来设置条形图的颜色、边框颜色、线宽等。如果你想绘制简单的水平条形图,你可以使用面向对象编程的方式,例如:import matplotlib.pyplot as plt import numpy as np plt.figure(figsize=(5,5)) ax=plt.axes() x=np.arange(0,6) y=np.random.random(6) ax.barh(x,y) plt.show()<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [matplotlibpyplot模块之柱状图(bar():基础参数、外观参数)](https://blog.csdn.net/mighty13/article/details/113869911)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [matplotlib.pyplot.bar()barh()绘制条形图](https://blog.csdn.net/Yangyuqing_/article/details/124180245)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

前端corner

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

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

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

打赏作者

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

抵扣说明:

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

余额充值