Pandas groupby plot gives first plot twice

petetheat

I have a dataframe with several categories and I want to use groupby to plot each category individually. However, the first category (or the first plot) is always plotted twice.

For example:

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

    n = 100000
    x = np.random.standard_normal(n)
    y1 = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
    y2 = 1.0 + 5.0 * x + 2.0 * np.random.standard_normal(n)

    df1 = pd.DataFrame({"A": x,
                        "B": y1})

    df2 =  pd.DataFrame({"A": x,
                         "B": y2})

    df1["Cat"] = "Cat1"
    df2["Cat"] = "Cat2"

    df = df1.append(df2, ignore_index=True)

    df.groupby("Cat").plot.hexbin(x="A", y="B",cmap = "jet")
    plt.show()

This will give me three plots, where Cat1 is plotted twice.

I just want two plots. What am I doing wrong?

Mathias711

This is expected behaviour, see the warning in the docs:

Warning: In the current implementation apply calls func twice on the first group to decide whether it can take a fast or slow code path. This can lead to unexpected behavior if func has side-effects, as they will take effect twice for the first group.

In your case, the plot function is called twice, which is visible in the result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas groupby plot gives first plot twice

From Java

GroupBy and plot with pandas

From Java

Pandas groupby two columns and plot

From Dev

Pandas groupby object in legend on plot

From Dev

Pandas groupby two columns and plot

From Dev

Keep order in plot of pandas groupby

From Dev

How to create Pandas groupby plot with subplots?

From Dev

pandas - plot according to groupby index level

From Dev

python pandas groupby plot with sorted date as xtick

From Dev

Pandas GroupBy object is not 'serializable' by Plot.ly

From Dev

Create Contour Plot from Pandas Groupby Dataframe

From Dev

How to plot aggregate results after groupby in Pandas?

From Dev

How to plot aggregate results after groupby in Pandas?

From Dev

Pandas loop over groupby and plot each group

From Dev

Pandas: How to label groupby data in plot?

From Dev

Python: Using pandas to import csv. Trying to plot a column but gives me an error saying "no numerical data to plot"

From Dev

Plot a stacked bar graph from two pandas groupby objects?

From Dev

Plot data returned from groupby function in Pandas using Matplotlib

From Dev

Plot a stacked bar graph from two pandas groupby objects?

From Dev

Legend on an empty plot gives no colors

From Dev

Filling the first plot with plotyy

From Java

How to groupby and plot aggregated data?

From Dev

How to plot data after groupby

From Dev

pandas DataFrame plot marker

From Dev

Pandas plot() without a legend

From Dev

Plot pandas dates in matplotlib

From Dev

Change the spacing of a pandas plot?

From Dev

plot dataframe pandas not working

From Dev

pandas plot - multiple colorbars