Repository: geopandas/geopandas

Issue:
BUG: UnboundLocalError when missing_kwds is used
I'm afraid the issue isn't completely solved yet. Still (0.9.0, 0.10.2) getting the following error  

<img width="709" alt="Bildschirmfoto 2021-11-01 um 18 21 00" src="https://user-images.githubusercontent.com/4449821/139724524-68bd0d0d-ad1c-405e-af3a-cb1f46155371.png">  

however, if I apply your fix a second time a little later in the code like this  

<img width="641" alt="Bildschirmfoto 2021-11-01 um 18 21 58" src="https://user-images.githubusercontent.com/4449821/139724615-f0264f23-76d2-4029-bbe7-4740d428f153.png">

it plots as expected – should you need a testcase pls let me know and thanks so far

_Originally posted by @wahlatlas in https://github.com/geopandas/geopandas/issues/1600#issuecomment-956497381_


Discussion:
Would this be of any help as a testcase
https://nbviewer.org/urls/wahlatlas.net/btw/21/geopandas.ipynb
or would you need a more reduced case with english comments?

<img width="954" alt="Bildschirmfoto 2021-11-01 um 20 17 01" src="https://user-images.githubusercontent.com/4449821/139728383-0171edd6-5dfe-434e-8e0d-6cb3dc1d65be.png">
Maybe some complete sentence on the issue:
The `missing_kwds` defines how missing data should be styled on the plotted map and does this just fine. However if such definition is present but there happens to be no missing data, the mentioned `UnboundLocalError` will occur. In the above example this comes up as the plot command is used with various data columns, some of which happen to have missing data, others not (in this case smaller parties that weren't eligible in the whole country).

Resolving patch:
diff --git a/geopandas/plotting.py b/geopandas/plotting.py
--- a/geopandas/plotting.py
+++ b/geopandas/plotting.py
@@ -860,7 +860,8 @@ def plot_dataframe(
             **style_kwds,
         )
 
-    if missing_kwds is not None and not expl_series[nan_idx].empty:
+    missing_data = not expl_series[nan_idx].empty
+    if missing_kwds is not None and missing_data:
         if color:
             if "color" not in missing_kwds:
                 missing_kwds["color"] = color
@@ -902,7 +903,7 @@ def plot_dataframe(
                         markeredgewidth=0,
                     )
                 )
-            if missing_kwds is not None:
+            if missing_kwds is not None and missing_data:
                 if "color" in merged_kwds:
                     merged_kwds["facecolor"] = merged_kwds["color"]
                 patches.append(


Test patch:
diff --git a/geopandas/tests/test_plotting.py b/geopandas/tests/test_plotting.py
--- a/geopandas/tests/test_plotting.py
+++ b/geopandas/tests/test_plotting.py
@@ -404,7 +404,7 @@ def test_categories(self):
         ):
             self.df.plot(column="cats", categories=["cat1"])
 
-    def test_misssing(self):
+    def test_missing(self):
         self.df.loc[0, "values"] = np.nan
         ax = self.df.plot("values")
         cmap = plt.get_cmap()
@@ -428,6 +428,12 @@ def test_misssing(self):
         np.testing.assert_array_equal(point_colors[0], leg_colors[0])
         np.testing.assert_array_equal(nan_color[0], leg_colors1[0])
 
+    def test_no_missing_and_missing_kwds(self):
+        # GH2210
+        df = self.df.copy()
+        df["category"] = df["values"].astype("str")
+        df.plot("category", missing_kwds={"facecolor": "none"}, legend=True)
+
 
 class TestPointZPlotting:
     def setup_method(self):