第18章 气泡图
气泡图
气泡图是一种散点图,其中标记大小用于表示数据的第三维度。
气泡图在 x 轴和 y 轴上分别展示两列数据,标记大小与第三个数值列成正比。
基础气泡图示例
下面是一个创建基础气泡图的示例。
创建
以下示例使用内置的 Plotly Studio 数据集,采用 weight、length 和 shipping_time 列,并套用上述提示结构:
创建 weight 对 length 的气泡图。根据 shipping time 设置大小。
Shipping time 计算为 shipped date 与 created date 之间的天数差。
带颜色维度的气泡图
通过增加颜色来表示第四列数据。
创建
以下示例使用内置的 Plotly Studio 数据集,采用 weight、length、shipping_time 和 defect 列,并套用上述提示结构:
创建 weight 对 length 的气泡图。
根据 shipping time 设置大小,根据 defect 设置颜色。
Shipping time 计算为 shipped date 与 created date 之间的天数差。
带连续颜色标度的气泡图
使用连续颜色标度将数值映射到颜色渐变。
创建
根据
使用
以下示例使用内置的 Plotly Studio 数据集,采用 weight、length 和 shipping_time 列,其中 shipping_time 同时用于大小和颜色:
创建 weight 对 length 的气泡图。
根据 shipping time 设置大小,根据 shipping time 设置颜色。
Shipping time 计算为 shipped date 与 created date 之间的天数差。
使用 Viridis 颜色标度。
交互式控件
添加下拉菜单及其他控件,使气泡图具有交互性。控件允许用户动态过滤和探索数据。
创建 weight 对 length 的气泡图。
根据 shipping time 设置大小,根据 defect 设置颜色。
Shipping time 计算为 shipped date 与 created date 之间的天数差。
添加下拉菜单选择 factory(选项:All, Osaka, Seoul, Singapore, Los Angeles, Montreal, Randstad)- 默认 All。
添加下拉菜单按 weight 范围过滤(选项:All, 0-1 kg, 1-2 kg, 2-3 kg)- 默认 All。
提示词关键字参考
在提示词中使用以下关键字和短语来自定义气泡图。
图表
以下关键字建议可用于创建和自定义图表:
| 关键字/短语 | 描述 | 示例 |
|---|---|---|
| X | 显示在水平轴上的变量 | weight |
| Y | 显示在垂直轴上的变量 | length |
| Size | 使气泡大小与数值变量成正比 | size by shipping time |
| Color | 按不同分组或连续值为气泡着色 | color by factory location |
| Facet columns | 为每个类别并排创建多个子图 | facet by factory location |
| Facet rows | 按组垂直堆叠创建多个图 | facet vertically by defect |
数据
在提示词中指定数据指令,以便在可视化前转换、过滤或聚合数据。
Calculate shipping time as the difference between shipped date and created date in days. Average weight by factory location.
以下关键字建议:
| 关键字/短语 | 描述 | 示例 |
|---|---|---|
| Aggregation | 指定如何聚合数据 | average weight by factory location |
| Computed field | 从现有数据创建新的计算字段 | calculate shipping time as the difference between shipped date and created date in days |
| Filter | 过滤数据以仅显示特定记录 | filter to show only defect = true |
选项
在提示词中指定选项,添加交互式控件,允许用户动态过滤、转换和可视化数据,而无需重新生成图表。
Add a dropdown to select factory (All, Osaka, Seoul) - Default All. Add a dropdown to filter by weight range (All, 0-1 kg, 1-2 kg, 2-3 kg) - Default All.
以下关键字建议。有关控件类型的完整列表和更多示例,请参阅 App Controls。
| 关键字/短语 | 描述 | 示例 |
|---|---|---|
| Dropdown | 添加下拉菜单按类别过滤 | Add a dropdown to select factory (All, Osaka, Seoul) - Default All |
图表样式
在提示词中指定图表样式,以控制气泡图的视觉效果和格式。
Use custom colors: #FF5733, #33FF57, #3357FF. Use Viridis color scale. Set opacity to 0.6. Add a linear trend line.
以下关键字建议:
| 关键字/短语 | 描述 | 示例 |
|---|---|---|
| Custom colors | 为类别或渐变指定精确颜色 | Use custom colors: #FF5733, #33FF57, #3357FF |
| Color scale | 指定连续颜色映射的颜色标度。请参阅内置颜色标度 | Use Viridis color scale |
| Reverse color scale | 反转颜色标度的方向 | Reverse color scale |
| Opacity | 气泡的透明度(0=不可见,1=实心) | Set opacity to 0.6 |
| Max bubble size | 设置气泡的最大尺寸 | Set maximum bubble size to 50 |
| Hover text | 悬停在气泡上时显示的内容 | Show serial number on hover |
| Axis labels | 重命名坐标轴标签以提高可读性 | Label x-axis as "Product Weight (kg)" |
将 y 轴标签设为“产品长度(cm)” 趋势线 添加一条展示整体趋势的直线 添加线性趋势线 对数刻度 数据范围过大时使用对数刻度(适用于指数型数据) 将 y 轴设为对数刻度 坐标轴范围 为坐标轴设置最小值和最大值 将 x 轴范围设为 0 到 3 图例 控制图例的显示与位置 在右上角显示图例