Skip to content

ENH: Let DataFrame.plot(kind="scatter") scatter plot each column against index if x/y not given. #51972

Open
@randolf-scholz

Description

@randolf-scholz

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

To my surprise, df.plot(kind="scatter") simply raises a ValueError complaining about missing "x" and "y".
In many cases, particularly time series data, the sensible thing to do is to scatter plot each channel against the index.

Feature Description

When x/y is not given scatter plot columns against the index.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

x = np.arange(0, 10, 0.1)
df = pd.DataFrame({"sin": np.sin(x), "cos": np.cos(x)}, index=x)

try:  # without subplots
    df.plot(kind="scatter")
except ValueError as e:
    print(e)
    fig, ax = plt.subplots()
    for col in df:
        ax.scatter(df.index, df[col], label=col)
    ax.legend()
    plt.show()

try:  # with subplots
    df.plot(subplots=True, kind="scatter")
except ValueError as e:
    print(e)
    fig, axes = plt.subplots(len(df.columns), 1)
    for ax, col in zip(axes, df):
        ax.scatter(df.index, df[col], label=col)
        ax.legend()
    plt.show()

Alternative Solutions

Of course, one could just do it manually.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions