mirror of
https://github.com/wassname/geopandas.git
synced 2026-10-03 12:31:14 +08:00
BUG: categories passed as a series are not properly sorted (#1670)
* add regression test * reindex if column is a Series when plotting * remove unnecessary import * add test for pd.Series colors w/o index too Co-authored-by: Nick Hand <nicholas.adam.hand@gmail.com>
This commit is contained in:
1 parent
9fdeb14741
commit
ca101aae93
2 files changed
+41
No files matched your search
@@ -636,6 +636,10 @@ def plot_dataframe(
|
||||
)
|
||||
else:
|
||||
values = column
|
||||
|
||||
# Make sure index of a Series matches index of df
|
||||
if isinstance(values, pd.Series):
|
||||
values = values.reindex(df.index)
|
||||
else:
|
||||
values = df[column]
|
||||
|
||||
|
||||
@@ -86,6 +86,43 @@ class TestPointPlotting:
|
||||
expected_colors = cmap(np.arange(self.N) / (self.N - 1))
|
||||
_check_colors(self.N, ax.collections[0].get_facecolors(), expected_colors)
|
||||
|
||||
def test_series_color_no_index(self):
|
||||
|
||||
# Color order with ordered index
|
||||
colors_ord = pd.Series(["a", "b", "c", "a", "b", "c", "a", "b", "c", "a"])
|
||||
|
||||
# Plot using Series as color
|
||||
ax1 = self.df.plot(colors_ord)
|
||||
|
||||
# Correct answer: Add as column to df and plot
|
||||
self.df["colors_ord"] = colors_ord
|
||||
ax2 = self.df.plot("colors_ord")
|
||||
|
||||
# Confirm out-of-order index re-sorted
|
||||
point_colors1 = ax1.collections[0].get_facecolors()
|
||||
point_colors2 = ax2.collections[0].get_facecolors()
|
||||
np.testing.assert_array_equal(point_colors1[1], point_colors2[1])
|
||||
|
||||
def test_series_color_index(self):
|
||||
|
||||
# Color order with out-of-order index
|
||||
colors_ord = pd.Series(
|
||||
["a", "a", "a", "a", "b", "b", "b", "c", "c", "c"],
|
||||
index=[0, 3, 6, 9, 1, 4, 7, 2, 5, 8],
|
||||
)
|
||||
|
||||
# Plot using Series as color
|
||||
ax1 = self.df.plot(colors_ord)
|
||||
|
||||
# Correct answer: Add as column to df and plot
|
||||
self.df["colors_ord"] = colors_ord
|
||||
ax2 = self.df.plot("colors_ord")
|
||||
|
||||
# Confirm out-of-order index re-sorted
|
||||
point_colors1 = ax1.collections[0].get_facecolors()
|
||||
point_colors2 = ax2.collections[0].get_facecolors()
|
||||
np.testing.assert_array_equal(point_colors1[1], point_colors2[1])
|
||||
|
||||
def test_colormap(self):
|
||||
|
||||
# without specifying values but cmap specified -> no uniform color
|
||||
|
||||
Reference in new issue
Block a user