Plotly 简明教程

Plotly - Dot Plots and Table

此处,我们将了解 Plotly 中的点图和 table 函数。首先,让我们从点图开始。

Dot Plots

点图在非常简单的刻度上显示点。它仅适合于少量的点,因为大量的点会使点图显得过于杂乱不清。点图还被称为 Cleveland dot plots 。它们显示两个(或更多个)时间点或两个(或更多个)条件之间的变化。

点图类似于水平条形图。然而,它们不会显得过于杂乱不清,并且允许更容易地在条件之间进行比较。该图形绘制了散点迹线,并将 mode 属性设置为 markers。

以下示例显示了在印度独立后每次人口普查中记录的男性和女性之间的识字率比较。图表中的两条迹线表示 1951 年至 2011 年期间每次人口普查中的男性和女性识字率百分比。

from plotly.offline import iplot, init_notebook_mode
init_notebook_mode(connected = True)
census = [1951,1961,1971,1981,1991,2001, 2011]
x1 = [8.86, 15.35, 21.97, 29.76, 39.29, 53.67, 64.63]
x2 = [27.15, 40.40, 45.96, 56.38,64.13, 75.26, 80.88]
traceA = go.Scatter(
   x = x1,
   y = census,
   marker = dict(color = "crimson", size = 12),
   mode = "markers",
   name = "Women"
)
traceB = go.Scatter(
x = x2,
y = census,
marker = dict(color = "gold", size = 12),
mode = "markers",
name = "Men")
data = [traceA, traceB]
layout = go.Layout(
   title = "Trend in Literacy rate in Post independent India",
   xaxis_title = "percentage",
   yaxis_title = "census"
)
fig = go.Figure(data = data, layout = layout)
iplot(fig)

输出应如下所示 −

cleveland dot plots

Table in Plotly

Plotly 的 Table 对象由 go.Table() 函数返回。表格迹线是一种图形对象,可用于以行和列的网格方式查看详细数据。表格使用按列优先的顺序,即网格表示为列向量的向量。

go.Table() 函数的两个重要参数是 header ,即表格的第一行,以及 cells ,即其余行。两个参数均为字典对象。标题的 values 属性是列标题的列表,以及列表的列表,每个列表对应于一行。

通过 linecolor、fill_color、font 和其他属性进一步定制样式。

以下代码显示了最近结束的 2019 年板球世界杯小组循环赛的积分表。

trace = go.Table(
   header = dict(
      values = ['Teams','Mat','Won','Lost','Tied','NR','Pts','NRR'],
      line_color = 'gray',
      fill_color = 'lightskyblue',
      align = 'left'
   ),
   cells = dict(
      values =
      [
         [
            'India',
            'Australia',
            'England',
            'New Zealand',
            'Pakistan',
            'Sri Lanka',
            'South Africa',
            'Bangladesh',
            'West Indies',
            'Afghanistan'
         ],
         [9,9,9,9,9,9,9,9,9,9],
         [7,7,6,5,5,3,3,3,2,0],
         [1,2,3,3,3,4,5,5,6,9],
         [0,0,0,0,0,0,0,0,0,0],
         [1,0,0,1,1,2,1,1,1,0],
         [15,14,12,11,11,8,7,7,5,0],
         [0.809,0.868,1.152,0.175,-0.43,-0.919,-0.03,-0.41,-0.225,-1.322]
      ],
      line_color='gray',
      fill_color='lightcyan',
      align='left'
   )
)
data = [trace]
fig = go.Figure(data = data)
iplot(fig)

输出如下所述 −

表格数据还可以从 Pandas 数据框中填充。我们按如下所示创建一个逗号分隔文件 ( points-table.csv ) −

Teams

Mat

Won

Lost

Tied

NR

Pts

NRR

India

9

7

1

0

1

15

0.809

Australia

9

7

2

0

0

14

0.868

England

9

6

3

0

0

14

1.152

New Zealand

9

5

3

0

1

11

0.175

Pakistan

9

5

3

0

1

11

-0.43

Sri Lanka

9

3

4

0

2

8

-0.919

South Africa

9

3

5

0

1

7

-0.03

Bangladesh

9

3

5

0

1

7

-0.41

Teams,Matches,Won,Lost,Tie,NR,Points,NRR
India,9,7,1,0,1,15,0.809
Australia,9,7,2,0,0,14,0.868
England,9,6,3,0,0,12,1.152
New Zealand,9,5,3,0,1,11,0.175
Pakistan,9,5,3,0,1,11,-0.43
Sri Lanka,9,3,4,0,2,8,-0.919
South Africa,9,3,5,0,1,7,-0.03
Bangladesh,9,3,5,0,1,7,-0.41
West Indies,9,2,6,0,1,5,-0.225
Afghanistan,9,0,9,0,0,0,-1.322

我们现在从此 csv 文件中构建一个数据框对象,并使用它按如下所示构建表格迹线 −

import pandas as pd
df = pd.read_csv('point-table.csv')
trace = go.Table(
   header = dict(values = list(df.columns)),
   cells = dict(
      values = [
         df.Teams,
         df.Matches,
         df.Won,
         df.Lost,
         df.Tie,
         df.NR,
         df.Points,
         df.NRR
      ]
   )
)
data = [trace]
fig = go.Figure(data = data)
iplot(fig)