散点图、Scattergl 图和气泡图
本章重点介绍散点图、散点图和气泡图的详细信息。首先,让我们研究一下散点图。
散点图
散点图用于 绘制数据 水平轴和垂直轴上的点以显示一个变量如何影响另一个变量。数据表中的每一行都由一个标记表示,该标记的位置取决于其在设置的列中的值 X and Y axes.
The 分散() graph_objs 模块的方法 (go.Scatter) 产生一个散射轨迹。在这里, mode 属性决定数据点的外观。 mode 的默认值是 lines,它显示连接数据点的连续线。如果设置为 markers ,仅显示由实心小圆圈表示的数据点。当模式被指定为“线条+标记”时,圆圈和线条都会显示。
在以下示例中,绘制了笛卡尔坐标系中三组随机生成点的散点轨迹。下面解释了以不同模式属性显示的每条迹线。
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 笔记本单元格 如下:
散点图
WebGL (Web Graphics Library) 是一个用于渲染交互的 JavaScript API 2D and 3D图形 在任何兼容的网络浏览器中,无需使用插件。 WebGL 与其他 Web 标准完全集成,允许图形处理单元 (GPU) 加速使用图像处理。
Plotly 你可以用 散点图() 代替 Scatter() 以提高速度、改进交互性以及绘制更多数据的能力。这 go.scattergl() 当涉及大量数据点时,该功能可提供更好的性能。
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)
输出如下:
气泡图
气泡图显示数据的三个维度。每个实体及其关联数据的三个维度被绘制为 disk (气泡)表示通过圆盘的两个维度 xy位置 第三个通过它的大小。气泡的大小由第三个数据系列中的值决定。
气泡图 是散点图的变体,其中数据点被替换为气泡。如果你的数据具有如下所示的三个维度,则创建气泡图将是一个不错的选择。
Company | Products | Sale | Share |
---|---|---|---|
A | 13 | 2354 | 23 |
B | 6 | 5423 | 47 |
C | 23 | 2451 | 30 |
气泡图是用 go.Scatter() 痕迹。上述数据系列中的两个作为 x 和 y 属性给出。第三个维度由标记显示,其大小代表第三个数据系列。在上述情况下,我们使用 products and sale as x and y 属性和 市场份额 as 标记大小 .
在 Jupyter 笔记本中输入以下代码。
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)
输出将如下图所示: