Lia A
Lia A

Reputation: 21

Setting legend location in matplotlib (while using geopandas)

I have tried several different methods to change my plot's legend's position, but none of them have worked. I would like to set the position for example to upper left or upper right.

I have a GeoDataFrame (data_proj) which has polygons in it. I want to plot only one map with those polygons.

I created my plot like this:

   p = data_proj.plot(column = "Level3", linewidth=0.03, legend = True)

I used these to set the title etc. for the legend:

  leg = p.get_legend()
  leg.set_title("Land cover")
  leg.get_frame().set_alpha(0)

How can I change the location of the legend?

Upvotes: 2

Views: 2265

Answers (2)

jdmcbr
jdmcbr

Reputation: 6406

On geopandas master (i.e., a change made subsequent to the current 0.3.0 release), a legend_kwds argument was added to the plot method. One can then do the following:

ax = df.plot(column='values', categorical=True, legend=True, legend_kwds={'loc': 2})

Upvotes: 3

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339062

In principle setting the legend should work as usual. The loc parameter can be used to define the location of the legend.

p = data_proj.plot(column = "Level3", linewidth=0.03)

leg = p.legend(loc="upper right")
leg.set_title("Land cover")
leg.get_frame().set_alpha(0)

Upvotes: 1

Related Questions