本文参考:https://pbpython.com/effective-matplotlib.html
原文比较啰嗦,本文简而言之。
1、介绍可视化关键词
2、介绍使用python的工具包的好处
3、奉献数据
4、撸代码
5、小结
①数据空间:是由n维属性和m个元素组成的数据集所构成的多维信息空间;
②数据开发:是指利用一定的算法和工具对数据进行定量的推演和计算;
③数据分析:指对多维数据进行切片、块、旋转等动作剖析数据,从而能多角度多侧面观察数据;
④数据可视化:是指将大型数据集中的数据以图形图像形式表示,并利用数据分析和开发工具发现其中未知信息的处理过程。
数据可视化已经提出了许多方法,这些方法根据其可视化的原理不同可以划分为基于几何的技术、面向像素技术、基于图标的技术、基于层次的技术、基于图像的技术和分布式技术等等。
上图,数据可视化的结果,脸基本长这样。
http://baijiahao.baidu.com/s?id=1598426519975889351&wfr=spider&for=pc
业界开源的一些数据可视化的工具。这些工具使用起来方便,但总会遇到工具无法实现的问题,所以要介绍下文。
Python的matplotlib等一系列开源包。
https://www.matplotlib.org.cn/ 中文文档,看这个就够了。
下面具个栗子:
获取数据:https://github.com/chris1610/pbpython/blob/master/data/sample-salesv3.xlsx?raw=true
数据如上图所示
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 本案例致力于探索matplotlib等数据可视化 # pandas是可视化的技术类库 import pandas as pd import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter df = pd.read_excel("C:/Users/XXXXX/Desktop/sample-salesv3.xlsx") 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) # 查看数据 # print(top_10.head()) # print(plt.style.available) # 使用ggplot绘图 # plt.style.use("ggplot") # top_10.plot(kind ="barh",y="Sales",x="Name") # plt.show() # 使用ax对象绘图 # fig, ax = plt.subplots() # top_10.plot(kind='barh', y="Sales", x="Name", ax=ax) # plt.show() # 设置坐标轴标签 # fig, ax = plt.subplots() # ax.set_xlim([-10000, 140000]) # ax.set_xlabel('Total Revenue') # ax.set_ylabel('Customer'); # ax.set(title="2014 revenue",xlable="total revenue",ylable="customer") # top_10.plot(kind='barh', y="Sales", x="Name", ax=ax) # plt.show() # 修改坐标周上的数据显示 def currency(x,pos): if x>=1000000: return '${:1.1f}M'.format(x*1e-6) return '${:1.0f}K'.format(x*1e-3) # 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') # avg = top_10["Sales"].mean() # for cust in [3, 5, 8]: # ax.text(115000, cust, "New Customer") # ax.axvline(x=avg, color='b', label='Average', linestyle='--', linewidth=1) # formatter = FuncFormatter(currency) # ax.xaxis.set_major_formatter(formatter) # ax.legend().set_visible(False) # plt.show() fig, (ax0, ax1) = plt.subplots(nrows=1,ncols=2, sharey=True, figsize=(7, 4)) top_10.plot(kind='barh', y="Sales", x="Name", ax=ax0) ax0.set_xlim([-10000, 140000]) ax0.set(title='Revenue', xlabel='Total Revenue', ylabel='Customers') # 绘制均值线 avg = top_10['Sales'].mean() ax0.axvline(x=avg, color='b', label='Average', linestyle='--', linewidth=1) # 重复绘制 top_10.plot(kind='barh', y="Purchases", x="Name", ax=ax1) avg = top_10['Purchases'].mean() ax1.set(title='Units', xlabel='Total Units', ylabel='') ax1.axvline(x=avg, color='b', label='Average', linestyle='--', linewidth=1) # 加标题 fig.suptitle('2014 Sales Analysis', fontsize=14, fontweight='bold'); # 隐藏图例 ax1.legend().set_visible(False) ax0.legend().set_visible(False) plt.show()
总结:
动手实践胜过读书万卷。