官术网_书友最值得收藏!

2.3 散點圖

2.3.1 基本案例

線形圖又稱為曲線圖,是最常用的圖形類型。與傳統(tǒng)的繪圖軟件不同,Plotly沒有獨立的線形圖函數(shù),而是把線形圖與散點圖全部用Scatter函數(shù)實現(xiàn)。

下面的代碼(見文件scatter_basic_demo.py)是基本散點圖的繪制方法,包括線形圖與散點圖兩種圖形的混合。

        import plotly as py
        import plotly.graph_objs as go

        # ----------pre def
        pyplt = py.offline.plot

        # ----------code
        # Create random data with numpy
        import numpy as np

        N = 100
        random_x = np.linspace(0, 1, N)
        random_y0 = np.random.randn(N)+5
        random_y1 = np.random.randn(N)
        random_y2 = np.random.randn(N)-5

        # Create traces
        trace0 = go.Scatter(
          x = random_x,
          y = random_y0,
          mode = 'markers', # 純散點的繪圖
          name = 'markers' # 曲線名稱
      )
      trace1 = go.Scatter(
          x = random_x,
          y = random_y1,
          mode = 'lines+markers', # “散點+線”的繪圖
          name = 'lines+markers'
      )
      trace2 = go.Scatter(
          x = random_x,
          y = random_y2,
          mode = 'lines', # 線的繪圖
          name = 'lines'
      )

      data = [trace0, trace1, trace2]
      pyplt(data, filename='tmp\scatter_basic_demo.html')

代碼運行結(jié)果如圖2-4所示。

圖2-4 代碼運行結(jié)果

查看案例源碼可以看出,markers、lines和lines+markers三個圖形的輸出格式不同,是因為Scatter函數(shù)中的mode參數(shù)不同:

        mode = 'markers', #xtr1,散點圖
        mode = 'lines', #xtr2,線形圖,
        mode = 'lines + markers', #xtr3,線形圖+散點圖組合

2.3.2 樣式設(shè)置

下面的代碼(見文件scatter_style.py)用來設(shè)置點的大小和顏色,以及線條的大小和顏色。

        import plotly as py
        import plotly.graph_objs as go

        import numpy as np

        # ----------pre def
        pyplt = py.offline.plot

        # ----------code
        N = 500
        x = np.random.randn(N)

        trace0 = go.Scatter(
            x = np.random.randn(N),
            y = np.random.randn(N)+2,
            name = 'Above',
            mode = 'markers+lines',
            marker = dict(
              size = 10, # 設(shè)置點的寬度
              color = 'rgba(152, 0, 0, .8)', # 設(shè)置點的顏色
              line = dict(
                  width = 2, # 設(shè)置線條的寬度
                  color = 'rgb(0, 0, 0)' # 設(shè)置線條的顏色
              )
            )
        )
    trace1 = go.Scatter(
        x = np.random.randn(N),
        y = np.random.randn(N) -2,
        name = 'Below',
        mode = 'markers',
        marker = dict(
          size = 10,
          color = 'rgba(255, 182, 193, .9)',
          line = dict(
              width = 2,
          )
        )
    )

    data = [trace0, trace1]

    layout = dict(title = 'Styled Scatter',
                yaxis = dict(zeroline = True), # 顯示y軸的0刻度線
                xaxis = dict(zeroline = False) # 不顯示x軸的0刻度線
                )

    fig = dict(data=data, layout=layout)
    pyplt(fig, filename='tmp\scatter_style.html')

代碼運行結(jié)果如圖2-5所示。

圖2-5 代碼運行結(jié)果

在這個案例中,重要的是marker參數(shù)的設(shè)置:

        marker = dict(
              size = 10, # 設(shè)置點的寬度
              color = 'rgba(152, 0, 0, .8)', # 設(shè)置點的顏色
              line = dict(
                  width = 2, # 設(shè)置線條的寬度
                  color = 'rgb(0, 0, 0)' # 設(shè)置線條的顏色
              )
            )

這里size設(shè)置的是點的寬度,width設(shè)置的是線條的寬度,第一個color設(shè)置的是點的顏色,第二個color設(shè)置的是線條的顏色,讀者可以對這些參數(shù)進(jìn)行修改,實際驗證一下。

需要注意的是,Plotly中的這些參數(shù)在其他繪圖函數(shù)中可以重復(fù)使用,這也是Plotly人性化的地方。

2.3.3 應(yīng)用案例

本案例的文件名是scatter_apply.py,講解Scatter函數(shù)的使用方法,繪制曲線圖與散點圖,以及曲線圖與散點圖的組合圖,代碼如下。

        # -*- coding: utf-8-*-

        import pandas as pd
        import pandas as pd
        import plotly as py
        import plotly.graph_objs as pygo

        # ----------pre def
        pd.set_option('display.width', 450)
        pyplt = py.offline.plot
        # ----------code
        df = pd.read_csv('dat/tk01_m15.csv')
        df9 = df[:10];
        print(df9)
        #
        idx = df9['xtim']
        xd0 = (df9['close'] -27) * 50
        df2 = df9
        df2['xd1'] = xd0-10
        df2['xd2'] = xd0
        df2['xd3'] = xd0 + 10

        print('xd2\n', df2);
        # --------
        xtr1 = pygo.Scatter(
            x=idx,
            y=df2['xd1'],
            mode='markers',  # xtr1,散點圖
            name='xtr1-markers',
        )
        xtr2 = pygo.Scatter(
            x=idx,
            y=df2['xd2'],
            mode='lines',  # xtr2,曲線圖
            name='xtr2-lines',
        )
        xtr3 = pygo.Scatter(
            x=idx,
            y=df2['xd3'],
            mode='markers+lines',  # xtr3,曲線圖+散點圖
            name='xtr3-markers+lines',
        )
        xdat = pygo.Data([xtr1, xtr2, xtr3])
        layout = pygo.Layout(
            title=’收盤價--15分鐘分時數(shù)據(jù)’,
            xaxis=pygo.XAxis(tickangle=-15),

        )
        fig = pygo.Figure(data=xdat, layout=layout, filename=r'tmp\
    scatter_apply.html')
        pyplt(fig)
        #
        print('ok')

代碼運行結(jié)果如圖2-6所示。

圖2-6 代碼運行結(jié)果

2.3.4 參數(shù)解讀

使用Scatter函數(shù)可以繪制線形圖與散點圖,主要參數(shù)如下。

●connectgaps:布爾變量,用于連接缺失數(shù)據(jù)。

●dx、dy:xy坐標(biāo)軸的步進(jìn)值,默認(rèn)值是1。

●error_x、error_y:xy出錯信息。

●fillcolor:填充顏色。

●fill:填充模式,包括格式、顏色等。

●hoverinfo:當(dāng)用戶與圖形互動時,鼠標(biāo)指針顯示的參數(shù),包括xyz坐標(biāo)數(shù)據(jù),以及text(文字信息)、name(圖形名稱)等參數(shù)的組合,可使用+、all、none和skip(忽略)作為組合連接符號,默認(rèn)是all(全部消失)。

●hoveron:當(dāng)用戶與圖形互動時,鼠標(biāo)指針顯示的模式,包括points(點圖)、fills(填充圖)和points+fills(點圖+填充圖)三種模式。

●ids:在動畫圖表中,數(shù)據(jù)點和圖形key鍵的列表參數(shù)。

●legendgroup:圖例參數(shù),默認(rèn)是空字符串。

●line:線條參數(shù),包括線條寬度、顏色、格式。

●marker:數(shù)據(jù)節(jié)點參數(shù),包括大小、顏色、格式等。

●mode:圖形格式,包括lines(線形圖)、markers(散點圖)和text(文本),使用+或none等符號進(jìn)行模式組合。

●name:名稱參數(shù)。

●opacity:透明度參數(shù),范圍是0~1。

●rsrc、xsrc、ysrc、tsrc、idssrc、textsrc、textpositionsrc:字符串源數(shù)組列表,可作為Plotly網(wǎng)格標(biāo)識符,用于設(shè)置特殊圖表所需的r參數(shù)、x參數(shù)、y參數(shù)、t參數(shù)、ids參數(shù)、text(文本)參數(shù)和textposition(文本位置)參數(shù)。

●r、t:僅用于極坐標(biāo)圖,r用于設(shè)置徑向坐標(biāo)(半徑),t用于設(shè)置角坐標(biāo)。

●showlegend:布爾變量,用于切換圖標(biāo)顯示。

●stream:數(shù)據(jù)流,用于實時顯示數(shù)據(jù)圖表。

●textfont:文本字體參數(shù),包括字體名稱、顏色、大小等。

●textposition:“文本”元素的位置參數(shù),包括top left(左上)、top center(中上)、top right(右上)、middle left(左中)、middle center(中心)、middle right(右中)、bottom left(左下)、bottom center(中下)、bottom right(右下)模式,默認(rèn)是middle center(中心)模式。

●text:文本數(shù)據(jù),設(shè)置與每個“(x, y)對”關(guān)聯(lián)的文本元素和數(shù)組列表格式,默認(rèn)是空字符串。

●type:數(shù)據(jù)顯示模式,包括constant(常數(shù))、percent(百分比)、sqrt(平方根)、array(數(shù)組)模式。

●x0、y0:坐標(biāo)軸起點坐標(biāo)。

●xaxis, yaxis:xy坐標(biāo)參數(shù)。

●xcalendar、ycalendar:坐標(biāo)時間參數(shù)的格式,默認(rèn)是公歷(gregorian),支持gregorian、chinese、coptic、discworld、ethiopian、hebrew、islamic、julian、mayan、nanakshahi、nepali、persian、jalali、taiwan、thai和ummalqura格式。

●x, y:設(shè)置xy軸的坐標(biāo)數(shù)據(jù)。

主站蜘蛛池模板: 大兴区| 越西县| 井研县| 枣阳市| 丘北县| 云龙县| 舒城县| 樟树市| 都兰县| 芜湖县| 凤阳县| 姜堰市| 长宁区| 宝山区| 富川| 天峻县| 镇康县| 霸州市| 全椒县| 洛宁县| 渝中区| 宁乡县| 临漳县| 宁乡县| 明星| 临桂县| 应城市| 开鲁县| 河池市| 邯郸县| 恩施市| 临夏县| 横峰县| 湟源县| 定远县| 油尖旺区| 隆安县| 晋城| 淮安市| 新丰县| 冕宁县|