将列表列编码为绘图的图例

Encoding a list column to the legend of a plot

提前致歉,我不确定如何最好地表达这个问题:

我正在处理一个大型数据集,我想绘制纬度和经度,其中点的颜色(实际上是不透明度)被编码为绑定到图例的 'FeatureType' 列。这样我就可以使用图例在我的地图上突出显示我正在寻找的各种特征。

Here is a picture of my map and legend so far

问题是在我的数据集中,FeatureType 列是可以在那里找到的特征列表(即拱门、桥梁等)。

我怎样才能使拱和桥都显示该点。目前它创建了自己的类别(拱门、桥梁等),导致大约 20 种不同的 FeatureTypes 的 300 多种组合。

可以在 http://atlantides.org/downloads/pleiades/dumps/pleiades-locations-latest.csv.gz

找到数据集

N.B:我正在使用 altair/pandas

import altair as alt
import pandas as pd
from vega_datasets import data


df = pd.read_csv ('C://path/pleiades-locations.csv') 

alt.data_transformers.enable('json')

countries = alt.topo_feature(data.world_110m.url, 'countries')

selection = alt.selection_multi(fields=['featureType'], bind='legend')

brush = alt.selection(type='interval', encodings=['x'])

map = alt.Chart(countries).mark_geoshape(
    fill='lightgray',
    stroke='white'
).project('equirectangular').properties(
    width=500,
    height=300
)

points = alt.Chart(df).mark_circle().encode(
    alt.Latitude('reprLat:Q'),
    alt.Longitude('reprLong:Q'),
    alt.Color('featureType:N'),
    tooltip=['featureType','timePeriodsKeys:N'],
    opacity=alt.condition(selection, alt.value(1), alt.value(0.0))
).add_selection(
    selection)

(map + points)

A​​ltair 无法从您当前的列格式生成您想要的标签。您需要将逗号分隔的字符串标签转换为列表,然后 explode the column 以便列表中的每个项目一行:

import altair as alt
import pandas as pd
from vega_datasets import data

alt.data_transformers.enable('data_server')

df = pd.read_csv('http://atlantides.org/downloads/pleiades/dumps/pleiades-locations-latest.csv.gz')[['reprLong', 'reprLat', 'featureType']]
df['featureType'] = df['featureType'].str.split(',')
df = df.explode('featureType')

countries = alt.topo_feature(data.world_110m.url, 'countries')

world_map = alt.Chart(countries).mark_geoshape(
    fill='lightgray',
    stroke='white')

points = alt.Chart(df).mark_circle(size=10).encode(
    alt.Latitude('reprLat:Q'),
    alt.Longitude('reprLong:Q'),
    alt.Color('featureType:N', legend=alt.Legend(columns=2)))

world_map + points

请注意,图例中有这么多条目没有意义,因为颜色是重复的。交互性会有所帮助,但我会考虑将其分成多个图表。我不确定是否有可能扩展图例以显示那些隐藏的 81 个条目。并仔细检查长纬度位置是否与您使用的世界地图投影正确对应,当我更改投影时它们似乎在四处移动。