- fig, ax = plt.subplots(figsize=(5, 6))
- top_10.plot(kind='barh', y="Sales", x="Name", ax=ax)
- ax.set_xlim([-10000, 140000])
- ax.set(title='2014 Revenue', xlabel='Total Revenue')
- ax.legend().set_visible(False)
基于很多原因你可能想要调剂一下这个图。看着最别扭的处所是总收入数字的格局。 Matplotlib可以经由过程FuncFormatter来帮我们实现。这个功能可以将用户定义的函数应用于值,并返回一个格局整洁的字符串放置在坐标轴上。
下面是一个泉币格局化函数,可以优雅地处收成十万范围内的美元格局:
- def currency(x, pos):
- 'The two args are the value and tick position'
- if x >= 1000000:
- return '${:1.1f}M'.format(x*1e-6)
- return '${:1.0f}K'.format(x*1e-3)
如今我们有一个格局化函数,须要定义它并将其应用到x轴。以下是完全的代码:
- fig, ax = plt.subplots()
- top_10.plot(kind='barh', y="Sales", x="Name", ax=ax)
- ax.set_xlim([-10000, 140000])
- ax.set(title='2014 Revenue', xlabel='Total Revenue', ylabel='Customer')
- formatter = FuncFormatter(currency)
- ax.xaxis.set_major_formatter(formatter)
- ax.legend().set_visible(False)
我们最后要去摸索的一个自定义功能是经由过程添加注释到画图。绘制一条垂直线,可以用ax.axvline()。添加自定义文本,可以用ax.text()。
起首,matplotlib有两种接口。第一种是基于MATLAB并应用基于状况的接口。第二种是面向对象的接口。为什么是这两种接口不在本文评论辩论典范围之内,然则知道有两种办法在应用matplotlib进行画图时异常重要。
在这个例子中,我们将绘制一条平均线,并显示三个新客户的标签。 下面是完全的代码和注释,把它们放在一路。
- # Create the figure and the axes
- fig, ax = plt.subplots()
- # Plot the data and get the averaged
推荐阅读
微服务架构:基于微服务和Docker容器技术的PaaS云平台架构设计(微服务架构实施原理)
基于微办事架构和Docker容器技巧的PaaS云平台扶植目标是给我们的开辟人员供给一套办事快速开辟、安排、运维治理、持续开辟持续集成的流程。平台供给基本举措措施、中心件、数据办事、云办>>>详细阅读
本文标题:高效使用Python可视化工具Matplotlib
地址:http://www.17bianji.com/lsqh/36034.html
1/2 1