Home > Back-end >  Spatial pie chart using geopandas
Spatial pie chart using geopandas

Time:04-18

I am trying to make a pie chart that looks like the below enter image description here-

I am using geopandas for that-

us_states = gpd.read_file("conus_state.shp")
data = gpd.read_file("data_file.shp")

fig, ax = plt.subplots(figsize= (10,10))
us_states.plot(color = "None", ax = ax)
data.plot(column = ["Column1","Column2"], ax= ax, kind = "pie",subplots=True)

This gives me the following error-

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\LSRATH~1.STU\AppData\Local\Temp/ipykernel_17992/1047905594.py in <module>
      1 fig, ax = plt.subplots(figsize= (10,10))
      2 us_states.plot(color = "None", ax = ax)
----> 3 diff_env.plot(column = ["WS_MON1","WS_MON2"], ax= ax, kind = "pie")

c:\python38\lib\site-packages\geopandas\plotting.py in __call__(self, *args, **kwargs)
    951         if kind in self._pandas_kinds:
    952             # Access pandas plots
--> 953             return PlotAccessor(data)(kind=kind, **kwargs)
    954         else:
    955             # raise error

c:\python38\lib\site-packages\pandas\plotting\_core.py in __call__(self, *args, **kwargs)
    921             if isinstance(data, ABCDataFrame):
    922                 if y is None and kwargs.get("subplots") is False:
--> 923                     raise ValueError(
    924                         f"{kind} requires either y column or 'subplots=True'"
    925                     )

ValueError: pie requires either y column or 'subplots=True'

Even after specifying, subplots = True, it does not work.

How can I make a pie chart using 2 columns of the dataframe?

Below are the first five rows of the relevant columns-

diff_env[["Column1", "Column2", "geometry"]].head().to_dict()

{'Column1': {0: 2, 1: 0, 2: 0, 3: 1, 4: 12},
 'Column2': {0: 2, 1: 0, 2: 0, 3: 1, 4: 12},
 'geometry': {0: <shapely.geometry.point.Point at 0x2c94e07f190>,
  1: <shapely.geometry.point.Point at 0x2c94e07f130>,
  2: <shapely.geometry.point.Point at 0x2c94e07f0d0>,
  3: <shapely.geometry.point.Point at 0x2c94bb86d30>,
  4: <shapely.geometry.point.Point at 0x2c94e07f310>}}

CodePudding user response:

  • you have not provided any usable sample data. Have randomly generated some
  • this is inspired by enter image description here

  • Related