使用 altair 在单个地图上绘制空间数据
Plotting spatial data on individual map using altair
我想在地图上绘制空间数据。我想用streamlit
,因为它似乎很容易使用,我想稍微探索一下。
首先尝试使用他们内置的 deck_gl
-API 绘制我的数据。这工作得很好,但是因为我需要绘制 glyphs/icons 并且 IconLayer
还没有内置到 streamlit
中,所以我需要切换到另一个库。
我读到 Altair
应该很适合我,streamlit
也很好地支持它。
但是,当我不使用 vega_datasets
.
之一时,我无法弄清楚如何使用 altair
创建可视化
我的数据位于具有以下结构的数据框中:
| latitude | longitude | temperature |
| ------------ | ------------- | --------------- |
| -122.23123 | 38.2321 | 23 |
| -122.2321 | 28.2342 | 25 |
如何使用 altair 创建这样的绘图?
非常感谢任何帮助或提示!
如果您有一个带有纬度和经度列的 pandas DataFrame,那么您可以在 Altair 中使用 latitude
和 longitude
编码通道。这是一个使用 vega_datasets 的示例(它看起来像您的数据)。
import altair as alt
from vega_datasets import data
df = data.airports()
df = df[df.state=='TX']
df.reset_index(inplace=True)
df.head()
alt.Chart(df).mark_point().encode(
latitude='latitude',
longitude='longitude',
color='index'
)
注意:确保您的 latitude
和 longitude
数据采用 WGS-84 (EPSG:4326) 投影。
目前向您的数据添加底图有点困难。我相信有人会想出一个新的好方法 image mark in Vega-lite (to be included in Altair 4), since there is already a working proof-of-concept in Vega (issue, editor) and in Vega-Lite (issue, editor).
更新:
要向您的地图添加更多上下文,您可以使用 geopandas 添加其他形状,例如:
import geopandas as gpd
gdf = gpd.read_file('https://raw.githubusercontent.com/python-visualization/folium/master/tests/us-states.json', driver='GeoJSON')
gdf = gdf[gdf.id=='TX']
base = alt.Chart(gdf).mark_geoshape(
stroke='gray',
fill=None
)
pts = alt.Chart(df).mark_point().encode(
latitude='latitude',
longitude='longitude',
color='index'
)
base + pts
我想在地图上绘制空间数据。我想用streamlit
,因为它似乎很容易使用,我想稍微探索一下。
首先尝试使用他们内置的 deck_gl
-API 绘制我的数据。这工作得很好,但是因为我需要绘制 glyphs/icons 并且 IconLayer
还没有内置到 streamlit
中,所以我需要切换到另一个库。
我读到 Altair
应该很适合我,streamlit
也很好地支持它。
但是,当我不使用 vega_datasets
.
altair
创建可视化
我的数据位于具有以下结构的数据框中:
| latitude | longitude | temperature |
| ------------ | ------------- | --------------- |
| -122.23123 | 38.2321 | 23 |
| -122.2321 | 28.2342 | 25 |
如何使用 altair 创建这样的绘图?
非常感谢任何帮助或提示!
如果您有一个带有纬度和经度列的 pandas DataFrame,那么您可以在 Altair 中使用 latitude
和 longitude
编码通道。这是一个使用 vega_datasets 的示例(它看起来像您的数据)。
import altair as alt
from vega_datasets import data
df = data.airports()
df = df[df.state=='TX']
df.reset_index(inplace=True)
df.head()
alt.Chart(df).mark_point().encode(
latitude='latitude',
longitude='longitude',
color='index'
)
注意:确保您的 latitude
和 longitude
数据采用 WGS-84 (EPSG:4326) 投影。
目前向您的数据添加底图有点困难。我相信有人会想出一个新的好方法 image mark in Vega-lite (to be included in Altair 4), since there is already a working proof-of-concept in Vega (issue, editor) and in Vega-Lite (issue, editor).
更新:
要向您的地图添加更多上下文,您可以使用 geopandas 添加其他形状,例如:
import geopandas as gpd
gdf = gpd.read_file('https://raw.githubusercontent.com/python-visualization/folium/master/tests/us-states.json', driver='GeoJSON')
gdf = gdf[gdf.id=='TX']
base = alt.Chart(gdf).mark_geoshape(
stroke='gray',
fill=None
)
pts = alt.Chart(df).mark_point().encode(
latitude='latitude',
longitude='longitude',
color='index'
)
base + pts