Giter Site home page Giter Site logo

Comments (9)

KinzigFlyer avatar KinzigFlyer commented on August 15, 2024 1

Thanks @KevsterAmp for looking into this

from pandas.

KinzigFlyer avatar KinzigFlyer commented on August 15, 2024 1

I don't think so,
Matplotlib does not provide stacking on it's own. You create a stacked bar by providing "bottom" parameter to the bars.
So I think bottom is calculated inside Pandas.
see this page in the official matplotlib documentation:
Stacked Bar charts

from pandas.

KinzigFlyer avatar KinzigFlyer commented on August 15, 2024 1

I took the official programm, changed one of the Above values to 0 and added the bar-label. Works correctly.

import matplotlib.pyplot as plt
import numpy as np

# data from https://allisonhorst.github.io/palmerpenguins/

species = (
    "Adelie\n $\\mu=$3700.66g",
    "Chinstrap\n $\\mu=$3733.09g",
    "Gentoo\n $\\mu=5076.02g$",
)
weight_counts = {
    "Below": np.array([70, 31, 58]),
    "Above": np.array([82, 0, 66]),
}
width = 0.5

fig, ax = plt.subplots()
bottom = np.zeros(3)

for boolean, weight_count in weight_counts.items():
    p = ax.bar(species, weight_count, width, label=boolean, bottom=bottom)
    bottom += weight_count

ax.bar_label(p, weight_counts['Above'])
ax.set_title("Number of penguins with above average body mass")
ax.legend(loc="upper right")

plt.show()
image

from pandas.

KinzigFlyer avatar KinzigFlyer commented on August 15, 2024 1

Converting the official example to a Pandas driven version shows the error:

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

# data from https://allisonhorst.github.io/palmerpenguins/
penguins = pd.DataFrame.from_dict([
    {'species': "Adelie", 'Below': 70.0, 'Above': 82.0},
    {'species': "Chinstrap", 'Below': 31.0, 'Above': 0.0},
    {'species': "Gentoo", 'Below': 58.0, 'Above': 66.0},
]).set_index('species')
width = 0.5

ax2 = penguins.plot.bar(stacked = True)

ax2.bar_label(ax2.containers[-1], penguins['Above'])
ax2.set_title("Number of penguins with above average body mass")
ax2.legend(loc="upper right")

plt.show()
image

from pandas.

KevsterAmp avatar KevsterAmp commented on August 15, 2024

take

from pandas.

KevsterAmp avatar KevsterAmp commented on August 15, 2024

All good, just waiting for this issue to be triaged by a maintainer before working on it.

from pandas.

KevsterAmp avatar KevsterAmp commented on August 15, 2024

@KinzigFlyer - seems like this is a problem on matplotlib not on the pandas itself

from pandas.

KevsterAmp avatar KevsterAmp commented on August 15, 2024

Good point, thank you

from pandas.

rhshadrach avatar rhshadrach commented on August 15, 2024

Thanks for the report - PRs to fix are welcome!

from pandas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.