用Python进行数据可视化的9种常见方法

讨论 埼玉老师
Lv4 准师级炼丹师
发布在 Matplotlib   1857   0
讨论 埼玉老师   1857   0

    如同艺术家们用绘画让人们更贴切的感知世界,数据可视化也能让人们更直观的传递数据所要表达的信息。

    我们今天就分享一下如何用 Python 简单便捷的完成数据可视化。

    其实利用 Python 可视化数据并不是很麻烦,因为 Python 中有两个专用于可视化的库 matplotlib 和 seaborn 能让我们很容易的完成任务。

    • Matplotlib:基于Python的绘图库,提供完全的 2D 支持和部分 3D 图像支持。在跨平台和互动式环境中生成高质量数据时,matplotlib 会很有帮助。也可以用作制作动画。
    • Seaborn:该 Python 库能够创建富含信息量和美观的统计图形。Seaborn 基于 matplotlib,具有多种特性,比如内置主题、调色板、可以可视化单变量数据、双变量数据,线性回归数据和数据矩阵以及统计型时序数据等,能让我们创建复杂的可视化图形。

    我们用 Python 可以做出哪些可视化图形?

    那么这里可能有人就要问了,我们为什么要做数据可视化?比如有下面这个图表:3

    当然如果你把这张图表丢给别人,他们倒是也能看懂,但无法很直观的理解其中的信息,而且这种形式的图表看上去也比较 low,这个时候我们如果换成直观又美观的可视化图形,不仅能突显逼格,也能让人更容易的看懂数据。

    下面我们就用上面这个简单的数据集作为例子,展示用 Python 做出9种可视化效果,并附有相关代码。

    导入数据集

    import matplotlib.pyplot as plt
    import pandas as pd
    df=pd.read_excel("E:/First.xlsx", "Sheet1")

    可视化为直方图

    fig=plt.figure() #Plots in matplotlib reside within a figure object, use plt.figure to create new figure
    #Create one or more subplots using add_subplot, because you can't create blank figure
    ax = fig.add_subplot(1,1,1)
    #Variable
    ax.hist(df['Age'],bins = 7) # Here you can play with number of bins
    Labels and Tit
    plt.title('Age distribution')
    plt.xlabel('Age')
    plt.ylabel('#Employee')
    plt.show()
    直方图

    可视化为箱线图

    import matplotlib.pyplot as plt
    import pandas as pd
    fig=plt.figure()
    ax = fig.add_subplot(1,1,1)
    #Variable
    ax.boxplot(df['Age'])
    plt.show()
    箱线图

    可视化为小提琴图

    import seaborn as sns 
    sns.violinplot(df['Age'], df['Gender']) #Variable Plot
    sns.despine()
    小提琴图

    可视化为条形图

    var = df.groupby('Gender').Sales.sum() #grouped sum of sales at Gender level
    fig = plt.figure()
    ax1 = fig.add_subplot(1,1,1)
    ax1.set_xlabel('Gender')
    ax1.set_ylabel('Sum of Sales')
    ax1.set_title("Gender wise Sum of Sales")
    var.plot(kind='bar')
    条形图

    可视化为折线图

    var = df.groupby('BMI').Sales.sum()
    fig = plt.figure()
    ax1 = fig.add_subplot(1,1,1)
    ax1.set_xlabel('BMI')
    ax1.set_ylabel('Sum of Sales')
    ax1.set_title("BMI wise Sum of Sales")
    var.plot(kind='line')
    折线图

    可视化为堆叠柱状图

    var = df.groupby(['BMI','Gender']).Sales.sum()
    var.unstack().plot(kind='bar',stacked=True,  color=['red','blue'], grid=False)
    堆叠柱状图

    可视化为散点图

    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.scatter(df['Age'],df['Sales']) #You can also add more variables here to represent color and size.
    plt.show()
    散点图

    可视化为泡泡图

    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.scatter(df['Age'],df['Sales'], s=df['Income']) # Added third variable income as size of the bubble
    plt.show()
    泡泡图

    可视化为饼状图

    var=df.groupby(['Gender']).sum().stack()
    temp=var.unstack()
    type(temp)
    x_list = temp['Sales']
    label_list = temp.index
    pyplot.axis("equal") #The pie chart is oval by default. To make it a circle use pyplot.axis("equal")
    #To show the percentage of each pie slice, pass an output format to the autopctparameter 
    plt.pie(x_list,labels=label_list,autopct="%1.1f%%") 
    plt.title("Pastafarianism expenses")
    plt.show()

    可视化为热度图

    import numpy as np
    #Generate a random number, you can refer your data values also
    data = np.random.rand(4,2)
    rows = list('1234') #rows categories
    columns = list('MF') #column categories
    fig,ax=plt.subplots()
    #Advance color controls
    ax.pcolor(data,cmap=plt.cm.Reds,edgecolors='k')
    ax.set_xticks(np.arange(0,2)+0.5)
    ax.set_yticks(np.arange(0,4)+0.5)
    # Here we position the tick labels for x and y axis
    ax.xaxis.tick_bottom()
    ax.yaxis.tick_left()
    #Values against each labels
    ax.set_xticklabels(columns,minor=False,fontsize=20)
    ax.set_yticklabels(rows,minor=False,fontsize=20)
    plt.show()

    你也可以自己试着根据两个变量比如性别(X 轴)和 BMI(Y 轴)绘出热度图。

    结语

    本文我们分享了如何利用 Python 及 matplotlib 和 seaborn 库制作出多种多样的可视化图形。通过上面的例子,我们应该可以感受到利用可视化能多么美丽的展示数据。而且和其它语言相比,使用 Python 进行可视化更容易简便一些。

    参考资料:
    https://www.analyticsvidhya.com/blog/2015/05/data-visualization-python/

    版权声明:作者保留权利,不代表意本站立场。如需转载请联系本站以及作者。

    参与讨论

    回复《 用Python进行数据可视化的9种常见方法

    EditorJs 编辑器

    沙发,很寂寞~
    反馈
    to-top--btn