Reputation: 33
I want to make a stacked bar chart from this data table:
Amino Acid | Agaricus bisporus | Aspergillus nidulans | Bipolaris maydis |
---|---|---|---|
CYS | 0 | 0 | 0 |
ASP | 0 | 0 | 0 |
GLU | 0 | 0 | 0 |
PHE | 0 | 0 | 0 |
GLY | 0 | 0 | 0 |
HIS | 0 | 0 | 0 |
ILE | 0 | 0 | 0 |
LYS | 10 | 7 | 16 |
LEU | 0 | 0 | 0 |
MET | 0 | 0 | 0 |
ASN | 9 | 15 | 15 |
PRO | 0 | 0 | 0 |
GLN | 11 | 13 | 4 |
ARG | 13 | 16 | 21 |
SER | 11 | 13 | 8 |
THR | 9 | 11 | 9 |
VAL | 0 | 0 | 0 |
TRP | 8 | 7 | 6 |
TYR | 9 | 6 | 7 |
I want the barchart to look like this:
I need the colours to represent the different Amino acids.
When I use the default plot setting this is what I get
Upvotes: 2
Views: 4071
Reputation: 80409
For a stacked barplot via pandas, each of the columns will be converted to a layer of bars. The index of the dataframe will be used as the x-axis.
In the given dataframe, you seem to want the columns for the x-axis. Using .T
to transpose the dataframe (exchanging rows and columns), will help. First, you'll need to set the amino acids as index.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_html('https://stackoverflow.com/questions/71654486/stacked-bar-chart-with-multiple-variables-in-python')[0]
ax = df.set_index('Amino Acid').T.plot.bar(stacked=True, rot=0, cmap='tab20', figsize=(10, 7))
ax.legend(bbox_to_anchor=(1.01, 1.02), loc='upper left')
plt.tight_layout()
plt.show()
Upvotes: 3