2

I made maps(plots) using matplotlib and geopandas. I have two dataframes, one contain whole country shapefile, and another one contain one of the district. like this;

df = geopandas.read_file('data/jamoat_exposure.shp')
df1 = df.query("DISTRICT=='Spitamen'")

I have to add both dataframe into single plot. So I placed them into same axis like this way

df.plot(ax=ax, color='white', edgecolor='grey', label='what')
df1.plot(column='AGRI_AREA', ax=ax, cmap='tab20', edgecolor='#000000', label=df1['AGRI_AREA'])

But it produce a map whose axis extended to df(country label). Then I set the xlim and ylim according to df1 geometry.

x1, y1, x2, y2 = df1.geometry.total_bounds
ax.set_xlim(x1-5000, x2+5000)
ax.set_ylim(y1-5000, y2+5000)

enter image description here

The color part is from df1 and another background lines are from df. This graph is for one district. If I choose the df1 as another district (df1=df.query(DISTICT='Ahuti')), It produce the graph having different axis. like this; enter image description here

I want all the map having same width and height (same figsize). I think, I have to add the axis limit dynamically so it will match the actual size. But I dont know how? Any idea?

CypherX
  • 7,019
  • 3
  • 25
  • 37
Tek Kshetri
  • 2,129
  • 1
  • 17
  • 41
  • Going off the answer in https://stackoverflow.com/questions/44970010/axes-class-set-explicitly-size-width-height-of-axes-in-given-units, there's a [fixed size axis demo](https://matplotlib.org/gallery/axes_grid1/demo_fixed_size_axes.html) that might work. Are each of your maps separate figures or are you using gridspec? (which by default makes axes of the same size) – story645 Dec 05 '19 at 04:04
  • Sorry, that is not the case for me. I want map having same width and height of axis for all the `DISTRICT` even the district `shapefile` are different – Tek Kshetri Dec 05 '19 at 04:52
  • so why does the demo not work for your task? also, will setting the axis equal distort the projection/geometry? (basically is this a valid thing to do for your data) – story645 Dec 05 '19 at 04:54
  • 1
    If x and y are eastings and northings then you can set_aspect(1). To make the subplots the same size do set_xlim/ylim to mean(x) + [-dx, dx] where dx is large enough to cover the largest map. – Jody Klymak Dec 05 '19 at 15:37
  • Sorry, I don't have to cover all the maps. I only have to display the `df1`. `df2` is just for base map. Due to variation in shape and size of geometry in `df1`, I am unable to set the map in same width and height axis. – Tek Kshetri Dec 06 '19 at 03:35
  • Thanks @jody. That is the exact answer what I want – Tek Kshetri Dec 06 '19 at 17:39

1 Answers1

3

Thanks @Jody. As your suggestion, I update my code as following. This work for me

max_range = np.array([x2-x1, y2-y1]).max()/2.0
axis.set_xlim((x1+x2)*0.5-max_range, (x1+x2)*0.5+max_range)
axis.set_ylim((y1+y2)*0.5-max_range, (y1+y2)*0.5+max_range)
Tek Kshetri
  • 2,129
  • 1
  • 17
  • 41