Skip to content

QST: DataFrame.plot() vs Series.plot() have inconsistency wrt reuse plot #59716

Open
@pollo-coder

Description

@pollo-coder

Research

  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.

Link to question on StackOverflow

https://stackoverflow.com/questions/72375976/odd-behavior-of-plotting-in-pandas

Question about pandas

I had asked this question on SO some time ago but got no satisfactory answer.

Essentially the question (slightly improved wrt what i asked on SO) was, after having imported necessary packages:

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

why this code reuses the figure (so i end up with just one figure)

ser = pd.Series(np.random.randn(100))
fig = plt.figure()
ser.plot()

while this one does not (and i end up with two figures)

df  = pd.DataFrame(np.random.randn(100,3))
fig = plt.figure()
df.plot()

?

Then i debugged a little bit and noticed that this is caused by the following two lines

if isinstance(data, ABCSeries):
    kwargs["reuse_plot"] = True

in the PlotAccessor.call in pandas/pandas/plotting/_core.py around line 990

Why do we need those two lines? What is the logic behind? Could we just remove them or set kwargs["reuse_plot"] always to True? I do not see the reason for the inconsistency.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions