如何获取作为geopandas数据框导入的shapefile的颜色条并在matplotlib中绘制?

时间:2018-03-20 23:47:19

标签: python matplotlib shapefile matplotlib-basemap geopandas

我有一个shapefile作为geopandas数据框导入,其中一个属性我用这种方式绘制:

{% import 'apostrophe-templates:areas.html' as areas %}

<div>
  {{ areas.defaultArea(data.page, 'body') }}
</div>

如何获取彩条?

剧情

enter image description here

打印(polydat.head(2))

#include <iostream>
#include <cstring>
using namespace std;
class myClass{
    private:
        char *ptr;
    public:
        myClass () {}
        myClass (char *str) 
        {
            ptr = new char[strlen(str)];
            strcpy(ptr, str);
        }
        myClass (const myClass &k) 
        {
            ptr= new char [strlen(k.ptr)+1];
            strcpy(ptr, k.ptr);
        }
        myClass& operator= (const myClass k)
        {
            delete [] ptr;
            ptr = new char [strlen(k.ptr)+1];
            strcpy(ptr, k.ptr);
            return *this;
        }
        ~myClass() {
            delete [] ptr;
        }
        void print() {
            cout<<*ptr;
        }
};

int main() {
    char s[6]="Hello";
    myClass p(s), m;
    m=p;
  m.print();
  return 0;
}

1 个答案:

答案 0 :(得分:1)

由于您已设置scheme='quantiles',因此您可能希望为图例使用分类色块。您可以使用legend选项并将其移到图表之外,如下所示:

import geopandas as gpd
import matplotlib.pyplot as plt

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

ax = world.plot(column='gdp_md_est', cmap='OrRd', scheme='quantiles', legend=True)
ax.get_legend().set_bbox_to_anchor((1.5, 1))

ax.set_axis_off()
plt.axis('equal')
plt.show()

enter image description here

如果你真的想要一个彩条,你可能需要自己编码。以下代码改编自geopandas

import geopandas as gpd
import matplotlib.pyplot as plt

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

ax = world.plot(column='gdp_md_est', cmap='OrRd', scheme='quantiles')

from matplotlib.colors import Normalize
from matplotlib import cm
norm = Normalize(vmin=world['gdp_md_est'].min(), vmax=world['gdp_md_est'].max())
n_cmap = cm.ScalarMappable(norm=norm, cmap='OrRd')
n_cmap.set_array([])
ax.get_figure().colorbar(n_cmap)

ax.set_axis_off()
plt.axis('equal')
plt.show()

enter image description here