diff --git a/geopandas/plotting.py b/geopandas/plotting.py index b3b6460..463ebd4 100644 --- a/geopandas/plotting.py +++ b/geopandas/plotting.py @@ -773,9 +773,11 @@ GON (((-122.84000 49.00000, -120.0000... if not show_interval: labels = [c[1:-1] for c in labels] - values = pd.Categorical([np.nan] * len(values), categories=labels, ordered=True) + values = pd.Categorical( + [np.nan] * len(values), categories=binning.bins, ordered=True + ) values[~nan_idx] = pd.Categorical.from_codes( - binning.yb, categories=labels, ordered=True + binning.yb, categories=binning.bins, ordered=True ) if cmap is None: cmap = "viridis" @@ -884,6 +886,8 @@ GON (((-122.84000 49.00000, -120.0000... norm = Normalize(vmin=mn, vmax=mx) n_cmap = cm.ScalarMappable(norm=norm, cmap=cmap) if categorical: + if scheme is not None: + categories = labels patches = [] for value, cat in enumerate(categories): patches.append( diff --git a/geopandas/tests/test_plotting.py b/geopandas/tests/test_plotting.py index 8e91ba1..cce912f 100644 --- a/geopandas/tests/test_plotting.py +++ b/geopandas/tests/test_plotting.py @@ -1073,6 +1073,8 @@ class TestMapclassifyPlotting: cls.df["mid_vals"] = np.linspace(0.3, 0.7, cls.df.shape[0]) cls.df["high_vals"] = np.linspace(0.7, 1.0, cls.df.shape[0]) cls.df.loc[cls.df.index[:20:2], "high_vals"] = np.nan + cls.nybb = read_file(get_path("nybb")) + cls.nybb["vals"] = [0.001, 0.002, 0.003, 0.004, 0.005] def test_legend(self): with warnings.catch_warnings(record=True) as _: # don't print warning @@ -1333,6 +1335,35 @@ class TestMapclassifyPlotting: line.get_markerfacecolor() for line in ax3.get_legend().get_lines() ] == legend_colors_exp + def test_equally_formatted_bins(self): + ax = self.nybb.plot( + "vals", + scheme="quantiles", + legend=True, + ) + labels = [t.get_text() for t in ax.get_legend().get_texts()] + expected = [ + "0.00, 0.00", + "0.00, 0.00", + "0.00, 0.00", + "0.00, 0.00", + "0.00, 0.01", + ] + assert labels == expected + + ax2 = self.nybb.plot( + "vals", scheme="quantiles", legend=True, legend_kwds=dict(fmt="{:.3f}") + ) + labels = [t.get_text() for t in ax2.get_legend().get_texts()] + expected = [ + "0.001, 0.002", + "0.002, 0.003", + "0.003, 0.003", + "0.003, 0.004", + "0.004, 0.005", + ] + assert labels == expected + class TestPlotCollections: def setup_method(self):