预备开端,我先惹人库并读入一些数据:
- import pandas as pd
- import matplotlib.pyplot as plt
- from matplotlib.ticker import FuncFormatter
- df = pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/sample-salesv3.xlsx?raw=true")
- df.head()
如不雅决定要把两幅图放在同一个图像上,我们应对若何做到这一点有根本懂得。 起首,创建图形,然后创建坐标轴,然后将其全部绘制在一路。我们可以用plt.subplots()来完成:
- top_10 = (df.groupby('name')['ext price', 'quantity'].agg({'ext price': 'sum', 'quantity': 'count'})
- .sort_values(by='ext price', ascending=False))[:10].reset_index()
- top_10.rename(columns={'name': 'Name', 'ext price': 'Sales', 'quantity': 'Purchases'}, inplace=True)
下面是数据的处理结不雅。
如许更美不雅,也是一个很好的例子,展示若何灵活地定义本身的问题解决筹划。
如今,数据被格局化成一个简单的表格,我们来看若何将这些结不雅绘制成条形图。
如前所述,matplotlib有很多不合的样式可用于衬着画图,可以用plt.style.available查看体系中有哪些可用的样式。
- plt.style.available
- ['seaborn-dark',
- 'seaborn-dark-palette',
- 'fivethirtyeight',
- 'seaborn-whitegrid',
- 'seaborn-darkgrid',
- 'seaborn',
- 'bmh',
- 'classic',
- 'seaborn-colorblind',
- 'seaborn-muted',
- 'seaborn-white',
- 'seaborn-talk',
- 'grayscale',
- 'dark_background',
- 'seaborn-deep',
推荐阅读
微服务架构:基于微服务和Docker容器技术的PaaS云平台架构设计(微服务架构实施原理)
基于微办事架构和Docker容器技巧的PaaS云平台扶植目标是给我们的开辟人员供给一套办事快速开辟、安排、运维治理、持续开辟持续集成的流程。平台供给基本举措措施、中心件、数据办事、云办>>>详细阅读
本文标题:高效使用Python可视化工具Matplotlib
地址:http://www.17bianji.com/lsqh/36034.html
1/2 1