-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Fix scatter plot colors in groupby context to match line plot behavior (#59846) #61233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ce37450
c3d388f
429a483
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,3 +152,52 @@ def test_groupby_hist_series_with_legend_raises(self): | |
|
||
with pytest.raises(ValueError, match="Cannot use both legend and label"): | ||
g.hist(legend=True, label="d") | ||
|
||
def test_groupby_scatter_colors_differ(self): | ||
# GH 59846 - Test that scatter plots use different colors for different groups | ||
# similar to how line plots do | ||
from matplotlib.collections import PathCollection | ||
import matplotlib.pyplot as plt | ||
|
||
# Create test data with distinct groups | ||
df = DataFrame( | ||
{ | ||
"x": [1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
"y": [1, 2, 3, 4, 5, 6, 7, 8, 9], | ||
"group": ["A", "A", "A", "B", "B", "B", "C", "C", "C"], | ||
} | ||
) | ||
|
||
# Set up a figure with both line and scatter plots | ||
fig, (ax1, ax2) = plt.subplots(1, 2) | ||
|
||
# Plot line chart (known to use different colors for different groups) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove. |
||
df.groupby("group").plot(x="x", y="y", ax=ax1, kind="line") | ||
|
||
# Plot scatter chart (should also use different colors for different groups) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove. |
||
df.groupby("group").plot(x="x", y="y", ax=ax2, kind="scatter") | ||
|
||
# Get the colors used in the line plot and scatter plot | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove. |
||
line_colors = [line.get_color() for line in ax1.get_lines()] | ||
|
||
# Get scatter colors | ||
scatter_colors = [] | ||
for collection in ax2.collections: | ||
if isinstance(collection, PathCollection): # This is a scatter plot | ||
# Get the face colors (might be array of RGBA values) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove. |
||
face_colors = collection.get_facecolor() | ||
# If multiple points with same color, we get the first one | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be useful here to explain why you're only getting the first one. |
||
if face_colors.ndim > 1: | ||
scatter_colors.append(tuple(face_colors[0])) | ||
else: | ||
scatter_colors.append(tuple(face_colors)) | ||
|
||
# Assert that we have the right number of colors (one per group) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove. |
||
assert len(line_colors) == 3 | ||
assert len(scatter_colors) == 3 | ||
|
||
# Assert that the colors are all different | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove. |
||
assert len(set(scatter_colors)) == 3 | ||
assert len(line_colors) == 3 | ||
|
||
plt.close(fig) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove.