Skip to content

BUG: Handle overlapping line and scatter on the same plot #61244

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

Merged
merged 12 commits into from
Apr 9, 2025

Conversation

MartinBraquet
Copy link
Contributor

@MartinBraquet MartinBraquet commented Apr 7, 2025

Running the code from the issue above now shows a correct plot.

import datetime
import matplotlib.pyplot as plt
import pandas as pd

datetime_list = [datetime.datetime(year=2025, month=1, day=1, hour=n) for n in range(23)]
y = [n for n in range(23)]
df = pd.DataFrame(columns=['datetime', 'y'])
for i, n in enumerate(datetime_list):
    df.loc[len(df)] = [n, y[i]]
fig, ax = plt.subplots(2, sharex=True)
df.plot.scatter(x='datetime', y='y', ax=ax[0])
df.plot(x='datetime', y='y', ax=ax[1])

image

Extract and streamline time series preparation steps into `prepare_ts_data`, replacing redundant logic across methods. Simplifies axis frequency handling and improves code readability while maintaining functionality.
This test ensures that the x-axis ticks are consistent between scatter and line plots when sharing the same axis. It addresses a potential issue related to GH#61005, verifying proper rendering of datetime x-axis labels.
This resolves an issue where line and scatter plots were not aligned when using Series.plot. The fix ensures proper alignment and improves plot consistency. Refer to issue pandas-dev#61005 for further details.
Datetime.time is now supported in scatter plots due to added converter implementation in ScatterPlot. Removed the test expecting a TypeError and updated it to validate the new functionality.
Simplify and streamline the code by directly assigning x_data from the data variable and replacing the intermediate Series object with a clearer `s` variable. This improves readability and maintains the existing functionality.
Relocated the `test_scatter_line_xticks` test from `test_series.py` to `test_frame.py` for better alignment with DataFrame-specific functionality. This refactor ensures the test resides in the appropriate context based on its usage and focus.
@@ -366,3 +364,17 @@ def format_dateaxis(
raise TypeError("index type not supported")

plt.draw_if_interactive()


def prepare_ts_data(data, ax, kwds):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to type this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 852 to 855
datetime_list = [datetime(year=2025, month=1, day=1, hour=n) for n in range(3)]
df = DataFrame(columns=["datetime", "y"])
for i, n in enumerate(datetime_list):
df.loc[len(df)] = [n, i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create this in 1 call? i.e. pass a dict to DataFrame

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created it by passing a list

@@ -54,6 +54,7 @@
)
from pandas.core.dtypes.missing import isna

from pandas import Series
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we conditionally import this where used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mroeschke mroeschke added the Visualization plotting label Apr 8, 2025
Added precise type annotations to the function signature for better clarity and type checking. Replaced `data` with `series` and `kwds` with `kwargs` to enhance readability and consistency.
The DataFrame creation in the test has been streamlined for clarity and conciseness by replacing the loop with a list comprehension. This improves code readability and maintains the same functionality.
Moved the `Series` import inside relevant function scopes to minimize unnecessary top-level imports and align with existing import patterns. This helps improve code readability and ensures imports are only loaded where needed.
Moved the import of `Series` within the `_make_plot` method to comply with styling or runtime considerations. This ensures consistency and avoids potential import-related issues.
@MartinBraquet
Copy link
Contributor Author

@mroeschke Thanks for the feedback; I applied your comments.

@mroeschke mroeschke added this to the 3.0 milestone Apr 9, 2025
@mroeschke mroeschke merged commit 920f996 into pandas-dev:main Apr 9, 2025
42 checks passed
@mroeschke
Copy link
Member

Thanks @MartinBraquet

@MartinBraquet MartinBraquet deleted the scatter branch April 9, 2025 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: scatter and line matplotlib plots not compatible for sharex=True datetime plots
2 participants