Comments (9)
Thanks @KevsterAmp for looking into this
from pandas.
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.
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](https://private-user-images.githubusercontent.com/43356610/355733593-62d8acec-b293-432d-8f4c-3968efbbac75.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjM2NzI4NTEsIm5iZiI6MTcyMzY3MjU1MSwicGF0aCI6Ii80MzM1NjYxMC8zNTU3MzM1OTMtNjJkOGFjZWMtYjI5My00MzJkLThmNGMtMzk2OGVmYmJhYzc1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE0VDIxNTU1MVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdkYTU5YmVmNDM3MmNhNDQxZDVjZjMxM2NjZGVhYzFmOTdhNWM0NWI0YmZiNjE0YzEzYWQ5ZTZiNTRhZWFkMjkmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.dvb0GfW1HMck03fdrElF9MuXV-qV955M4r2uWr8CJok)
from pandas.
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](https://private-user-images.githubusercontent.com/43356610/355745563-45640f42-fb4a-4371-8b8e-153a9d4a194b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjM2NzI4NTEsIm5iZiI6MTcyMzY3MjU1MSwicGF0aCI6Ii80MzM1NjYxMC8zNTU3NDU1NjMtNDU2NDBmNDItZmI0YS00MzcxLThiOGUtMTUzYTlkNGExOTRiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE0VDIxNTU1MVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQ2MjRmMGYzYTJjZDc5MDUzYmZjNTE0OGFhMTMwMjE0MWU3ZjdkM2I1MDFhOTEyN2NhNDVhZjhiNDdiZGM1YjImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.0SOUWYHyH3bHysXzlS2p-UzU0WztLF5FzhOaXO434D4)
from pandas.
take
from pandas.
All good, just waiting for this issue to be triaged by a maintainer before working on it.
from pandas.
@KinzigFlyer - seems like this is a problem on matplotlib not on the pandas itself
from pandas.
Good point, thank you
from pandas.
Thanks for the report - PRs to fix are welcome!
from pandas.
Related Issues (20)
- BUG: Series.replace(dict-like, dict-like) raises uninformative AttributeError HOT 1
- BUG: escapechar=',' Causes Double Commas in Output in Pandas 2.2.2 HOT 2
- DOC: fix docstring validation errors for `pandas.Timestamp` HOT 15
- DOC: Development on Gitpod have problems HOT 1
- BUG: Ability to set both color and style in pandas plotting HOT 2
- Title: Feature Request: Improve diff Function to Support Forward and Backward CompletionENH: HOT 2
- Title: Feature Request: Improve diff Function to Support Forward and Backward CompletionENH: HOT 2
- Bug in Chunk Processing: Non-NULL IDs Become NULL During IterationBUG: HOT 2
- ENH: Add Float128 support for groupby. HOT 1
- PERF: Excessive memory consumption in pd.read_parquet HOT 2
- BUG: Resampling to `"B"` frequency with `closed="right"` and `label="right"` adds empty bins
- BUG: ArrowNotImplementedError: Unsupported cast from int64 to null using function cast_null HOT 1
- ENH: Adding `skipna:bool` to Rolling.sum HOT 2
- BUG: Replace fails after NaN in a Series of `string` HOT 2
- DEPR: future.no_silent_downcasting option HOT 1
- BUILD: Python Docker Build Issues HOT 4
- DOC: Incorrect Documentation: pd.Dataframe.dropna HOT 2
- BUG: Spurious `FutureWarning` when using `pd.read_json()` HOT 2
- BUG: read_json silently ignores the dtype when engine=pyarrow HOT 1
- ENH: support the Arrow PyCapsule Interface on pandas.Series (export) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pandas.