1 简介

开门见山,今天我们要模仿的数据可视化作品来自 「#TidyTuesday」 活动于2020年1月28日发布的「旧金山街道树木数据集」下的众多参赛作品中,由Philippe Massicotte创作的(如图1所示)非常受欢迎的 「Street trees of San Francisco」

图1

原作者使用的工具是R语言,而今天的文章内容,我就将带大家学习如何在Python中模仿图1的风格进行类似数据信息的可视化展示(其实原作品有一些令人困惑的瑕疵,因此我在下文中在一些地方采用了与原作者不同的分析方式,因此最终的成品与原作品有一些不同之处)。

2 模仿过程

今天我们要模仿的这张图,咋一看上去似乎略复杂,但如果你曾经阅读过我的「基于geopandas的空间数据分析」系列文章,就一下子可以在脑中将此图构成进行分解:

2.1 过程分解

我们仔细观察原作品,可以get到其主要视觉元素是将统计出的数值映射到每个社区面色彩之上,且外围的轮廓描边,很明显是整个地区对应整体的向外缓冲区,再辅以道路网,使得整张图看起来显得很“精密”。

结合我们手头的数据:旧金山社区「面」数据、有登记的街道树木「点」数据,至于道路网「线」数据我们则可以利用第三方库osmnx进行获取(建议利用conda install -c conda-forge osmnx进行安装)。

将过程拆分为下列步骤:

  • 「数据准备」

首先我们需要读入已有的数据并进行相应的矢量化:

图2

而路网数据我们则可以利用osmnx进行在线获取,只需传入我们的旧金山面数据bbox范围,配合

osmnx进行获取即可:

图3

接着我们在上述数据基础上对每个社区面内部的街道树木数量进行统计并对数据进行分箱,配上预设区间的色彩值:

# 统计每个社区内部的树木数量
sf_trees = \
(
    gpd
    # 空间连接
    .sjoin(left_df=sf,
           right_df=trees,
           op='contains',
           how='left')
    # 按照name分组计数(这里未连接到任何数的社区被
    # 记为1本质上是错误的,但我们绘图分段后这一点不影响)
    .groupby('name')
    .agg({
        'name''count',
        'geometry''first'
    })
    .rename(columns={'name''数量'})
    .reset_index(drop=False)
    # 直接转为GeoDataFrame
    .pipe(gpd.GeoDataFrame, crs='EPSG:4326')
)

sf_trees['颜色'] = (
    pd
    .cut(sf_trees['数量'], 
         bins=[025005000750010000, max(sf_trees['数量'])], 
         labels=['#e4f1e1''#c0dfd1''#67a9a2''#3b8383''#145e64'])
)

最后别忘记了我们作为轮廓的缓冲区生成:

# 生成轮廓缓冲区
sf_bounds = gpd.GeoSeries([sf.buffer(0.001).unary_union], crs='EPSG:4326')
  • 「主要视觉元素绘制」

做好这些准备后我们直接就可以先将图像的主体元素绘制出来:

import matplotlib.pyplot as plt
from matplotlib import font_manager as fm

# 设置全局默认字体
plt.rcParams['font.sans-serif'] = ['Times New Roman']

fig, ax = plt.subplots(figsize=(66))

# 设置背景色
ax.set_facecolor('#333333')
fig.set_facecolor('#333333')

# 图层1:缓冲区轮廓
ax = (
    sf_bounds
    .plot(ax=ax, facecolor='none', edgecolor='#cccccc', linewidth=1)
)

# 图层2:带有树木统计信息的社区面
ax = (
    sf_trees
    .plot(color=sf_trees['颜色'], edgecolor='#333333',
          linewidth=0.5, ax=ax)
)

# 图层3:osm路网
ax = (
    roads
    .plot(linewidth=0.05, edgecolor='#3c3d3d',
          ax=ax)
)

# 设置x轴
ax.set_xticks([-122.5-122.45-122.4-122.35])
ax.set_xticklabels(['122.5°W''122.45°W''122.4°W''122.35°W'])

# 设置y轴
ax.set_yticks([37.7237.7437.7637.7837.837.82])
ax.set_yticklabels(['37.72°N''37.74°N''37.76°N''37.78°N''37.8°N''37.82°N'])

# 设置坐标轴样式
ax.tick_params(axis='both', labelcolor='#737373', color='none', labelsize=8)

# 隐藏周围的spines线条
ax.spines['left'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('none')

# 导出图像
fig.savefig('图4.png', dpi=600, bbox_inches='tight')

图4
  • 「辅助视觉元素的添加」

接下来我们只需要补充上各种点睛之笔的小元素即可,其中值得一提的是下方的图例我们用inset_axes()插入子图的方式灵活实现。

并且外部字体文件的使用也是很添彩的,我们这里就分别在「标题」「刻度标签」处使用到了两种特殊的字体(你可以在开头的Github仓库找到我用到的所有字体文件):

fig, ax = plt.subplots(figsize=(66))

# 设置背景色
ax.set_facecolor('#333333')
fig.set_facecolor('#333333')

# 图层1:缓冲区轮廓
ax = (
    sf_bounds
    .plot(ax=ax, facecolor='none', edgecolor='#cccccc', linewidth=1)
)

# 图层2:带有树木统计信息的社区面
ax = (
    sf_trees
    .plot(color=sf_trees['颜色'], edgecolor='#333333',
          linewidth=0.5, ax=ax)
)

# 图层3:osm路网
ax = (
    roads
    .plot(linewidth=0.05, edgecolor='#3c3d3d',
          ax=ax)
)

# 设置x轴
ax.set_xticks([-122.5-122.45-122.4-122.35])
ax.set_xticklabels(['122.5°W''122.45°W''122.4°W''122.35°W'])

# 设置y轴
ax.set_yticks([37.7237.7437.7637.7837.837.82])
ax.set_yticklabels(['37.72°N''37.74°N''37.76°N''37.78°N''37.8°N''37.82°N'])

# 设置坐标轴样式
ax.tick_params(axis='both', labelcolor='#737373', color='none', labelsize=8)

# 隐藏周围的spines线条
ax.spines['left'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('none')

# 以插入子图的方式添加下方图例
ax_bar = ax.inset_axes((0.25-0.120.50.015))
ax_bar.set_facecolor('#333333')
ax_bar.spines['left'].set_color('none')
ax_bar.spines['right'].set_color('none')
ax_bar.spines['top'].set_color('none')
ax_bar.spines['bottom'].set_color('none')

ax_bar.bar(range(5), [1]*5, width=0.975, color=['#e4f1e1''#c0dfd1''#67a9a2''#3b8383''#145e64'])
ax_bar.set_yticks([])
ax_bar.set_xticks([i+0.5 for i in range(4)])
ax_bar.set_xticklabels(['2500''5000''7500''10000'], 
                       fontdict={'fontproperties': fm.FontProperties(fname="RobotoCondensed-Regular.ttf")})
ax_bar.tick_params(color='none', labelcolor='#ffffff', labelsize=8, pad=0)

ax.set_title('Street trees of San Francisco'
             fontsize=24,
             color='#ffffff',
             pad=40,
             fontproperties=fm.FontProperties(fname="Amaranth-Bold.ttf"))

ax.text(0.51.08'''There are a total of 192987 trees in San Francisco regrouped into 571 species.
The district with the most number of trees is Mission whereas the one with
the least number of trees is LincoLn Park / Ft. Miley.'''
, transform=ax.transAxes, ma='center',
        ha='center', va='top', color='#ffffff')

ax.text(0.5-0.22'Visualization by CNFeffery', fontsize=8,
        color='#737373', ha='center', transform=ax.transAxes)

# 导出图像
fig.savefig('图5.png', dpi=600, bbox_inches='tight')

图5

以上就是本文的全部内容,欢迎在评论区与我进行讨论~


©著作权归作者所有:来自51CTO博客作者mb5fe18fab305a5的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 在pandas中使用数据透视表
  2. 在模仿中精进数据可视化03:OD数据的特殊可视化方式
  3. 在模仿中精进数据可视化02:温室气体排放来源可视化
  4. Vaex :突破pandas,快速分析100GB大数据集
  5. 在pandas中利用hdf5高效存储数据
  6. 在模仿中精进数据可视化01:国内38城居住自由指数
  7. 多快好省地使用pandas分析大型数据集
  8. 原来Python自带了数据库,用起来真方便!
  9. 基于geopandas的空间数据分析——空间计算篇(上)

随机推荐

  1. 基于Platinum库的DMR实现(android)
  2. android进度条的样式
  3. android ListView 中getview学习总结
  4. Android(安卓)安装配置及其项目开发
  5. Android启动续-------SystemSever启动
  6. Android自用-----Android中一些关于Activ
  7. Android之路——第二个Android小程序(Andr
  8. Android imageView图片按比例缩放
  9. freescale Android(安卓)Camera 调试总结
  10. Android(安卓)Paint和Color类