美文网首页
Python 数据可视化:绘图类的应用

Python 数据可视化:绘图类的应用

作者: you的日常 | 来源:发表于2020-12-07 17:16 被阅读0次

    Plotly 的 plotly.graph_objs 库提供了很多种绘图类,涵盖了常用的统计图表,并且还有所拓展。不过,为了对类的理解更深刻,以下讲述将按照对象进行,不同的统计图依据相应的参数来实现。

    4.2.1 Scatter 类——绘制直线图、散点图

    在 plotly.graph_objs 中的 Scatter 类,功能比较多,用它能够绘制直线图、散点图等。

    根据经验,可以在 Jupyter 中输入了 go.Scatter,然后按下 TAB 键,查看到不仅仅是 Scatter,还包含其他以 Scatter 开头的家族成员(如下图所示)。

    image

    不过在这里,我们仅关注 go.Scatter,其他对象暂时不研究。

    go.Scatter(arg=None, cliponaxis=None, connectgaps=None, customdata=None, customdatasrc=None, dx=None, dy=None, error_x=None, error_y=None, fill=None, fillcolor=None, groupnorm=None, hoverinfo=None, hoverinfosrc=None, hoverlabel=None, hoveron=None, hovertext=None, hovertextsrc=None, ids=None, idssrc=None, legendgroup=None, line=None, marker=None, mode=None, name=None, opacity=None, orientation=None, r=None, rsrc=None, selected=None, selectedpoints=None, showlegend=None, stackgaps=None, stackgroup=None, stream=None, t=None, text=None, textfont=None, textposition=None, textpositionsrc=None, textsrc=None, tsrc=None, uid=None, unselected=None, visible=None, x=None, x0=None, xaxis=None, xcalendar=None, xsrc=None, y=None, y0=None, yaxis=None, ycalendar=None, ysrc=None, **kwargs)
    
    

    面对这么多参数,不要惊慌,前面学习过程中,也不是没有见过这种阵势。参数多,说明它的功能比较全。如果有兴趣,可以根据官方文档的说明,将每个参数的含义通读一遍。若暂时没兴趣或没时间,就到用的时候再去看吧。

    下面就用一些示例来说明某些参数的含义(示例中的数据来自:https://github.com/qiwsir/DataSet/tree/master/universityrank)。

    import pandas as pd
    import plotly
    import plotly.graph_objs as go
    
    df = pd.read_csv("/Users/qiwsir/Documents/Codes/DataSet/universityrank/timesData.csv")
    df.head()
    
    
    world_rank university_name country teaching international research citations income total_score num_students student_staff_ratio international_students female_male_ratio year
    0 1 Harvard University United States of America 99.7 72.4 98.7 98.8 34.5 96.1 20,152 8.9 25% NaN 2011
    1 2 California Institute of Technology United States of America 97.7 54.6 98.0 99.9 83.7 96.0 2,243 6.9 27% 33 : 67 2011
    2 3 Massachusetts Institute of Technology United States of America 97.8 82.3 91.4 99.9 87.5 95.6 11,074 9.0 33% 37 : 63 2011
    3 4 Stanford University United States of America 98.3 29.5 98.1 99.2 64.3 94.3 15,596 7.8 22% 42 : 58 2011
    4 5 Princeton University United States of America 90.9 70.3 95.4 99.9 - 94.2 7,929 8.4 27% 45 : 55 2011

    现在读入的数据集是世界各大学排名,在后续示例中,只取其中的前一百所大学。

    df100 = df.iloc[:100, :]
    
    

    首先要做的,就是利用 go.Scatter 创建 Trace 对象,将此对象提交给 Plotly 的 API 之后,Plotly 服务器就能根据此 Trace 对象的有关配置,返回相应的图线,即可得到所要绘制的图示。

    trace1 = go.Scatter(x = df100['world_rank'],    # 世界排名
                        y = df100['citations'],        # 被引用次数
                        mode = 'lines',
                        name = 'citations', 
                        marker = dict(color='rgba(16, 112, 2, 0.8)'),
                        text = df100['university_name']
                       )
    trace2 = go.Scatter(x = df100['world_rank'],
                        y = df100['teaching'],
                        mode = 'lines+markers',
                        name = 'teaching',
                        marker = dict(color='rgba(80, 26, 80, 0.8)'),
                        text = df100['university_name']
                       )
    trace3 = go.Scatter(x = df100['world_rank'],
                        y = df100['research'],
                        mode = 'markers',
                        name = 'research',
                        marker = dict(color='rgba(40, 66, 120, 0.8)'),
                        text = df100['university_name']
                       )
    data = [trace1, trace2, trace3]
    layout = dict(title = "Citation Research and Teaching VS World Rank of Top100 Universities",
                  xaxis = dict(title='Wrold Rank', ticklen=5, zeroline=False)
                 )
    fig = dict(data=data, layout=layout)
    
    plotly.offline.init_notebook_mode(connected=True)
    plotly.offline.iplot(fig)
    
    

    输出结果:

    enter image description here

    在这里绘制的是三种图,控制不同类型图的参数是 go.Scatter 中的 mode,它的值可以是:

    • 'lines',折线图,没有标记坐标点;
    • 'lines + markers',折线图,标记坐标点;
    • 'markers',散点图,没有各点之间的连线。

    如果觉得观察起来有点乱,可以通过图示的交互功能,比如选择图例,显示指定的图线。

    另外几个参数的含义,也简要解释一下:

    相关文章

      网友评论

          本文标题:Python 数据可视化:绘图类的应用

          本文链接:https://www.haomeiwen.com/subject/ollebktx.html