## matplotlib.pyplot库的知识点之bar函数——绘制条形图

matplotlib.pyplot库的知识点之bar函数——绘制条形图

Help on BarContainer in module matplotlib.container object:

class BarContainer(Container)
 |  Container for the artists of bar plots (e.g. created by `.Axes.bar`).
 |  
 |  The container can be treated as a tuple of the *patches* themselves.
 |  Additionally, you can access these and further parameters by the
 |  attributes.

I
参数设置(我只关心常用的):

plt.bar(left,height,width=0.8,color,label)

left,height不可缺省

left,height:序列类型,常常是列表形式(元组或列表),分别表示x轴的位置序列和y轴的数值序列。示例:

>>> import matplotlib.pyplot as plt 
>>> plt.bar([1,2,3,4,5],(10,20,30,40,50))
<BarContainer object of 5 artists>

>>> plt.show()

注意到left和height直接显示在x,y轴上
left和height包含的元素可以是字符串:

>>> plt.bar((10,20,30,40,50),['apple','banana','pear','grape','melon'])
<BarContainer object of 5 artists>
>>> plt.bar(['apple','banana','pear','grape','melon'],(10,20,30,40,50))
<BarContainer object of 5 artists>
>>> plt.show()

在这里插入图片描述
在这里插入tihujjjjhjhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh图片描述

width:整数或浮点数类型,缺省值为0.8。

>>> plt.bar([1,2,3,4,5],(10,20,30,40,50),width=1)
<BarContainer object of 5 artists>
>>> plt.show()

在这里插入图片描述
color:字符串类型,常用的有’red’,‘green’,等等,也可用RGB颜色值表示,也是字符串。
label:图像的标签,字符串类型。它是为legend()函数做铺垫的。
II
有关的一些函数
1.
plt.lengend()
不需要参数,有时考察loc这一参数
2.
plt.xlabels(labels,fontsize)
plt.xlabels(labels,fontsize)
参数类型:分别为字符串,整数
3.
plt.xticks(numpy.arange(a,b,n),labels,rotation)
plt.yticks(numpy.arange(a,b,n),labels,rotation)

labels:往往是字符串的列表或元组,注意n=len(labels),
rotation:整数或浮点数
4.
plt.title(title,fontsize)
5.
plt,savefig(name)
III
中文字码问题
如果参数中出现中文,图象会乱码:

>>> plt.bar(['甲','乙','丙','丁','戊'],[10,20,30,40,50])
	     
<BarContainer object of 5 artists>
>>> plt.show()

在这里插入图片描述
解决方案:

>>> import matplotlib.pyplot as plt 
>>> plt.rcParams['font.sans-serif']=['KaiTi']
>>>xstring='甲乙丙丁戊'
>>>xstring=xstring.decode('u-tf8')
>>>left=xstring.split('')
>>> plt.bar(left,[10,20,30,40,50])
	     
<BarContainer object of 5 artists>
>>> plt.show()

这是老师给的方法,不过我暂且存疑。

欢迎使用Markdown编辑器

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

新的改变

我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

  1. 全新的界面设计 ,将会带来全新的写作体验;
  2. 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示;
  3. 增加了 图片拖拽 功能,你可以将本地的图片直接拖拽到编辑区域直接展示;
  4. 全新的 KaTeX数学公式 语法;
  5. 增加了支持甘特图的mermaid语法1 功能;
  6. 增加了 多屏幕编辑 Markdown文章功能;
  7. 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间;
  8. 增加了 检查列表 功能。

功能快捷键

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值