Plotly 简明教程
Scatter Plot, Scattergl Plot and Bubble Charts
本章重点介绍散点图、Scattergl 图和气泡图的详细信息。首先,让我们探讨散点图。
This chapter emphasizes on details about Scatter Plot, Scattergl Plot and Bubble Charts. First, let us study about Scatter Plot.
Scatter Plot
散点图用于将点 plot data 在水平轴和垂直轴上,以展示一个变量如何影响另一个变量。数据表中的每一行都用一个标记表示,其位置取决于其在 X 和 Y 轴上设置的列中的值。
Scatter plots are used to plot data points on a horizontal and a vertical axis to show how one variable affects another. Each row in the data table is represented by a marker whose position depends on its values in the columns set on the X and Y axes.
graph_objs 模块 (go.Scatter) 的 scatter() 方法产生一个散点轨迹。此处, mode 属性决定了数据点的外观。模式的默认值为 lines,显示连接数据点的连续线。如果设置为 markers ,则只显示由小空心圆点表示的数据点。当模式指定为“lines+markers”时,则显示圆形和线。
The scatter() method of graph_objs module (go.Scatter) produces a scatter trace. Here, the mode property decides the appearance of data points. Default value of mode is lines which displays a continuous line connecting data points. If set to markers, only the data points represented by small filled circles are displayed. When mode is assigned ‘lines+markers’, both circles and lines are displayed.
在以下示例中,绘制了笛卡尔坐标系中三组随机生成点的散点轨迹。下面解释了每条轨迹显示的不同的模式属性。
In the following example, plots scatter traces of three sets of randomly generated points in Cartesian coordinate system. Each trace displayed with different mode property is explained below.
import numpy as np
N = 100
x_vals = np.linspace(0, 1, N)
y1 = np.random.randn(N) + 5
y2 = np.random.randn(N)
y3 = np.random.randn(N) - 5
trace0 = go.Scatter(
x = x_vals,
y = y1,
mode = 'markers',
name = 'markers'
)
trace1 = go.Scatter(
x = x_vals,
y = y2,
mode = 'lines+markers',
name = 'line+markers'
)
trace2 = go.Scatter(
x = x_vals,
y = y3,
mode = 'lines',
name = 'line'
)
data = [trace0, trace1, trace2]
fig = go.Figure(data = data)
iplot(fig)
Jupyter notebook cell 的输出如下所示:
The output of Jupyter notebook cell is as given below −
Scattergl Plot
WebGL (网络图形库)是一个 JavaScript API,用于在任何兼容的网络浏览器中呈现交互式 2D 和 3D graphics ,而无需使用插件。WebGL 与其他网络标准完全集成,允许图形处理单元 (GPU) 加速使用图像处理。
WebGL (Web Graphics Library) is a JavaScript API for rendering interactive 2D and 3D graphics within any compatible web browser without the use of plug-ins. WebGL is fully integrated with other web standards, allowing Graphics Processing Unit (GPU) accelerated usage of image processing.
Plotly 您可以使用 Scattergl() 代替 Scatter() 实现 WebGL,以提高速度、改善交互性,并绘制更多数据的能力。 go.scattergl() 函数在涉及大量数据点时可以提供更好的性能。
Plotly you can implement WebGL with Scattergl() in place of Scatter() for increased speed, improved interactivity, and the ability to plot even more data. The go.scattergl() function which gives better performance when a large number of data points are involved.
import numpy as np
N = 100000
x = np.random.randn(N)
y = np.random.randn(N)
trace0 = go.Scattergl(
x = x, y = y, mode = 'markers'
)
data = [trace0]
layout = go.Layout(title = "scattergl plot ")
fig = go.Figure(data = data, layout = layout)
iplot(fig)
输出如下:
The output is mentioned below −
Bubble charts
气泡图显示数据的三个维度。具有关联数据的三个维度的每个实体均绘为一个 disk (气泡),它通过圆盘的 xy location 表示两个维度,并通过其大小表示第三个维度。气泡的大小由第三个数据序列中的值决定。
A bubble chart displays three dimensions of data. Each entity with its three dimensions of associated data is plotted as a disk (bubble) that expresses two of the dimensions through the disk’s xy location and the third through its size. The sizes of the bubbles are determined by the values in the third data series.
Bubble chart 是散点图的一种变化形式,其中数据点用气泡代替。如果你的数据包含三个维度,如下所示,则创建一个气泡图是一个不错的选择。
Bubble chart is a variation of the scatter plot, in which the data points are replaced with bubbles. If your data has three dimensions as shown below, creating a Bubble chart will be a good choice.
Company |
Products |
Sale |
Share |
A |
13 |
2354 |
23 |
B |
6 |
5423 |
47 |
C |
23 |
2451 |
30 |
气泡图用 go.Scatter() 迹生成。以上两个数据序列中的 products 和 sale 用 作 x 和 y 属性,而 market share 作为 marker size 。
Bubble chart is produced with go.Scatter() trace. Two of the above data series are given as x and y properties. Third dimension is shown by marker with its size representing third data series. In the above mentioned case, we use products and sale as x and y properties and market share as marker size.
在 Jupyter 笔记本中输入以下代码。
Enter the following code in Jupyter notebook.
company = ['A','B','C']
products = [13,6,23]
sale = [2354,5423,4251]
share = [23,47,30]
fig = go.Figure(data = [go.Scatter(
x = products, y = sale,
text = [
'company:'+c+' share:'+str(s)+'%'
for c in company for s in share if company.index(c)==share.index(s)
],
mode = 'markers',
marker_size = share, marker_color = ['blue','red','yellow'])
])
iplot(fig)
输出应如下所示 −
The output would be as shown below −