2

I am trying add hatching over specific polygons that represent counties in a state in the US. Each polygon is filled in correlation to the amount of grocery stores that are in that specific county. The hatching would represent 5 levels (range of highest to lowest) of diabetes rates for adults for each of the counties.

I've come across various possible ways of implementing this but each time I come away kind of stumped.

-I have seen a post on here where someone used the Descartes package to draw an individual polygon, but like other posts I've seen regarding this, I'm not sure if I have to use GeoJSON coordinates for the polygon? I attained coordinates for each polygon through my pyshp package, are these the right coordinates to use? I feel like they are not..

Descartes example: https://gis.stackexchange.com/questions/197945/geopandas-polygon-to-matplotlib-patches-polygon-conversion

example of the coordinates from pyshp package:

from area import area
sf = shapefile.Reader("county_az.shp")
Shapes = sf.shapes()
bbox = Shapes[3].bbox

#1
[-12296461.118197802, 3675955.2075050175,  -12139158.....]

#2
[-12618534.046562605, 4063552.9669038206, -12328686.313377801, ....]

#3
[-12436701.142463798, 3893144.9847336784, -12245216.013980595, ...]

-When I come across contour plots, my problem is that it is intended for the entire plot, I cannot limit the lat and long range for a specific polygon because there are not enough values. I believe I have the same issue with clipping as well.

As seen here:Hatch area using pcolormesh in Basemap

I should note that my data for my plot is a shapefile, and I have columns regarding Diabetes rates for adults for 2010, grocery stores per county as well as other variables.

Is there a way to hatch over individual polygons on a basemap?

Bjortt
  • 23
  • 3

1 Answers1

2

If shape is the shape from a .shp file, you can supply it to a matplotlib.patches.Polygon and add some hatch with the hatch argument.

p= Polygon(np.array(shape), fill=False, hatch="X")
plt.gca().add_artist(p) 

A complete example:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import numpy as np

m = Basemap(llcrnrlon=-10,llcrnrlat=35,urcrnrlon=35,urcrnrlat=60.,
             resolution='i', projection='tmerc', lat_0 = 48.9, lon_0 = 15.3)

m.drawcoastlines()

# shape file from 
# http://www.naturalearthdata.com/downloads/10m-cultural-vectors/10m-admin-0-countries/
fn = r"ne_10m_admin_0_countries\ne_10m_admin_0_countries"
m.readshapefile(fn, 'shf', drawbounds = False) 
# here, 'shf' is the name we later use to access the shapes.

#Madrid
x,y = m([-3.703889],[40.4125])
m.plot(x,y, marker="o", color="k", label="Madrid", ls="")

hatches = ["\\\\","++", "XX"]
countries = ['Spain', 'Ireland', "Belgium"]
hatchdic = dict(zip(countries, hatches))
shapes = {}
for info, shape in zip(m.shf_info, m.shf):
    if info['NAME'] in countries:
        p= Polygon(np.array(shape), fill=False, hatch=hatchdic[info['NAME']])
        shapes.update({info['NAME'] : p})

for country in countries:
    plt.gca().add_artist(shapes[country]) 

handles, labels = plt.gca().get_legend_handles_labels()
handles.extend([shapes[c] for c in countries])  
labels.extend(countries)     
plt.legend(handles=handles, labels=labels, handleheight=3, handlelength=3, framealpha=1. )

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Oh my! Thank you so much! =^_^= – Bjortt May 30 '17 at 19:06
  • What is an .shf file though? Is this a file format that I can get through QGIS? I've never seen this in ArcGIS. – Bjortt May 30 '17 at 19:11
  • 1
    Sorry, I meant `.shp`. In this case it's the `ne_10m_admin_0_countries.shp` downloaded from naturalearthdata.com. I added that information. I used the name `shf` to access the shapefiles and got confused. ;-) – ImportanceOfBeingErnest May 30 '17 at 20:25