0

I am using GeoPandas and I want to plot two layers. I want the map to have the extent of the smaller layer. With this code below, the map's size automatically fits the bigger layer.

import geopandas as gp
import matplotlib.pyplot as plt
from shapely.geometry import LineString
from shapely.geometry import MultiPoint

thesmallpoints=gp.GeoDataFrame([[MultiPoint([(0, 0), (1, 1), (1,2), (2,2)])]],columns=['geometry'])
thelargeline=gp.GeoDataFrame([[LineString([(0, 0), (1, 1), (1,2), (20,20)])]],columns=['geometry'])

base=thesmallpoints.plot(marker='o', color='blue', markersize=20)
thelargeline.plot(ax=base, color='green')
plt.show()

How can this be achieved?

2

You can set limits to the axis without chaining original GDF.

import geopandas as gp
import matplotlib.pyplot as plt
from shapely.geometry import LineString, MultiPoint, Polygon

thesmallpoints=gp.GeoDataFrame([[MultiPoint([(0, 0), (1, 1), (1,2), (2,2)])]],columns=['geometry'])
thelargeline=gp.GeoDataFrame([[LineString([(0, 0), (1, 1), (1,2), (20,20)])]],columns=['geometry'])

minx, miny, maxx, maxy = thesmallpoints.geometry.total_bounds


f, ax = plt.subplots()
thesmallpoints.plot(ax=ax, marker='o', color='blue', markersize=20)
thelargeline.plot(ax=ax, color='green')
ax.set_xlim(minx - .1, maxx + .1) # added/substracted value is to give some margin around total bounds
ax.set_ylim(miny - .1, maxy + .1)
plt.show()
0

So I solved the problem by clipping the large linestring as follows

import geopandas as gp
import matplotlib.pyplot as plt
from shapely.geometry import LineString, MultiPoint, Polygon

thesmallpoints=gp.GeoDataFrame([[MultiPoint([(0, 0), (1, 1), (1,2), (2,2)])]],columns=['geometry'])
thelargeline=gp.GeoDataFrame([[LineString([(0, 0), (1, 1), (1,2), (20,20)])]],columns=['geometry'])
minx, miny, maxx, maxy = thesmallpoints.geometry.total_bounds
thesmallpointsenvelope = gp.GeoDataFrame([[Polygon([(minx, miny), (minx, maxy), (maxx, maxy), (maxx,miny), (minx, miny)])]], columns=['geometry'])
thelargeline = thelargeline.intersection(thesmallpointsenvelope.unary_union)
base=thesmallpoints.plot(marker='o', color='blue', markersize=20)
thelargeline.plot(ax=base, color='green')
plt.show()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.