has2k1 / has2k1/plotnine

geom_bar (stacked bars) is slow

Open
#339 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement Performance
Dominant language
Python
Stars
4.8k
Forks
254
Avg merge
10h 35m
Merged PRs (30d)
9

Description

First, thanks for the nice package.

I was working on a problem and noticed the plotnine implementation of geom_bar is slow especially for stacked bar plots. Below is an example:

from plotnine import ggplot, geom_bar, aes
import pandas as pd
import random

n = 4000
data = pd.DataFrame(
    {
        "a": range(0, n),
        "u": random.sample(range(1, 2 * n), n),
        "v": random.sample(range(1, 2 * n), n),
        "w": random.sample(range(1, 2 * n), n),
        "x": random.sample(range(1, 2 * n), n),
        "y": random.sample(range(1, 2 * n), n),
    }
)

data1 = pd.melt(data, id_vars=["a"], var_name="sty", value_name="value")
p = ggplot() + geom_bar(aes(x="a", weight="value", fill="sty"), data=data1)

%timeit p.save("test.pdf")

On my machine, it takes about 30 seconds to make a plot. Yet, the similar code in R would takes only about 5 seconds. The R code is attached below.

library(ggplot2)
library(data.table)
library(rbenchmark)

n = 4000
data = data.table(a=1:n,
                  u=runif(n), v=runif(n), w=runif(n), x=runif(n), y=runif(n))

data1 = melt(data, id.vars="a", variable.name="sty", value.name="value")

p = ggplot() + geom_bar(aes(x=a,weight=value,fill=sty), data=data1)

benchmark(ggsave("test1.pdf", p), replications=10)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Python benchmark in the issue with geom_bar and p.save("test.pdf"), then trace the geom_bar rendering path involved in stacked bars. Compare the result with the reported roughly 30-second runtime and identify where time is spent; done means materially improving stacked-bar plot generation while preserving the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data-visualization, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.