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包含的元素可以是字符串:
>>> 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()
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编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:
- 全新的界面设计 ,将会带来全新的写作体验;
- 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示;
- 增加了 图片拖拽 功能,你可以将本地的图片直接拖拽到编辑区域直接展示;
- 全新的 KaTeX数学公式 语法;
- 增加了支持甘特图的mermaid语法1 功能;
- 增加了 多屏幕编辑 Markdown文章功能;
- 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间;
- 增加了 检查列表 功能。
功能快捷键
撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift