当前位置:   article > 正文

GeoPandas 笔记: GeoDataFrame.plot()_geopandas plot

geopandas plot

0 数据

  1. import geopandas as gpd
  2. world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
  3. world

 1 画图

1.1 基础plot

world.plot()

 1.2 column:根据geoDataFrame的哪一列来进行着色

world.plot(column='gdp_md_est')

 1.3 legend

给出颜色的图例

  1. world.plot(column='gdp_md_est',
  2. legend=True)

 1.3.1 调整legend 相对于图的布局

  1. from mpl_toolkits.axes_grid1 import make_axes_locatable
  2. import matplotlib.pyplot as plt
  3. fig, ax = plt.subplots(1, 1)
  4. divider = make_axes_locatable(ax)
  5. cax = divider.append_axes("right", size="5%", pad=0.1)
  6. world.plot(column='pop_est', ax=ax, legend=True, cax=cax)

 1.3.2 legend_kwd 布局

  1. world.plot(column='pop_est',
  2. legend=True,
  3. legend_kwds={'label': "gdp_md_est",
  4. 'orientation': "horizontal"})

1.4 Cmap 颜色 

world.plot(column='pop_est', cmap='OrRd');

 

 1.5 boundary 绘制轮廓

world.boundary.plot()

1.6 scheme 颜色映射的放缩方式

 可供选择的选项:“box_plot”、“equal_interval”、“fisher_jenks”、“fisher_jenks_sampled”、“headtail_breaks”、“jenks_caspall”、“jenks_caspall_forced”、“jenks_caspall_sampled”、“max_p_classifier”、“maximum_breaks”、“natural_breaks”、“quantiles”、“percentiles”、“std_mean”、“user_defined”

  1. world.plot(column='pop_est',
  2. cmap='OrRd',
  3. scheme='quantiles')

 1.7 set_axis_off 不显示边框

  1. world.plot(column='pop_est',
  2. cmap='OrRd',
  3. scheme='quantiles').set_axis_off()

 1.8 缺失值处理

1.8.1 构造缺失值

  1. import numpy as np
  2. world.loc[np.random.choice(world.index, 40), 'pop_est'] = np.nan
  3. #选择40个区域,使其值变成nan

1.8.2 直接plot

会发现Nan的部分直接消失了

world.plot(column='pop_est');

1.8.3 missing_kwds

  1. world.plot(column='pop_est',
  2. missing_kwds={'color': 'lightgrey'});

 

 

  1. world.plot(
  2. column="pop_est",
  3. missing_kwds={
  4. "color": "lightgrey",
  5. "edgecolor": "red",
  6. "hatch": "///"
  7. },
  8. );

 2 在一种图上叠加另外一种图

2.0 另一个数据的导入

  1. cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
  2. cities

 

2.1 实现方法1:不使用matplotlib

  1. ax=world.boundary.plot()
  2. cities.plot(ax=ax,marker='*',color='red',markersize=10)

2.2 实现方法2:使用matplotlib

  1. fig,ax=plt.subplots(1,1)
  2. world.boundary.plot(ax=ax)
  3. cities.plot(ax=ax,marker='*',color='red',markersize=10);

 

 3 绘制其他图

3.1 line——线图

world.plot(kind='line', x="pop_est")

 

3.2 bar——柱状图

world.plot(kind='bar', x="pop_est")

 

 3.3 hist——直方图

world.plot(kind='hist', x="pop_est")

 3.4 box——箱式图

world.plot(kind='box', x="pop_est")

 

3.5 kde——密度图

world.plot(kind='kde', x="pop_est")

 

3.6 area——面积图

3.7 scatter——散点图

world.plot(kind='scatter', x="pop_est",y='gdp_md_est')

 

 

3.8 hexbin——六边形箱图

world.plot(kind='hexbin', x="pop_est",y='gdp_md_est')

 

 

3.9 pie——饼图

world.plot(kind='pie', x="pop_est", y="gdp_md_est")

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/322586
推荐阅读
相关标签
  

闽ICP备14008679号