3

I have a dataframe with a varying number of columns. With positive and negative values. Is it possible to make a stacked bar chart with all of these columns in the dataframe?

enter image description here

    A    B     C
0   34   34    -12
1   34   223   -12
2  34    56    -12
3   34   86    -12
4   56   86    -12
5   56   43    -12
6   78   34    -12
0

3 Answers 3

8

(Updated answer for newer versions of plotly)

Using px.bar will give you a stacked bar chart directly. If your bar chart for some reason is not stacked, try:

fig.update_layout(barmode='stack')

enter image description here

Complete code:

import pandas as pd
import plotly.express as px

# data
df = pd.DataFrame({'A': {0: 34, 1: 34, 2: 34, 3: 34, 4: 56, 5: 56, 6: 78},
                     'B': {0: 34, 1: 223, 2: 56, 3: 86, 4: 86, 5: 43, 6: 34},
                     'C': {0: -12, 1: -12, 2: -12, 3: -12, 4: -12, 5: -12, 6: -12}})

colors = px.colors.qualitative.T10

# plotly
fig = px.bar(df, 
             x = df.index,
             y = [c for c in df.columns],
             template = 'plotly_dark',
             color_discrete_sequence = colors,
             title = 'Stacked bar chart using px.bar()', 
             )

fig.show()

Old answer:


Here's a stacked bar chart using plotly offline in a Jupyter Notebook. Just make sure you have the packages listed under imports installed. df = cf.datagen.lines() produces a pandas dataframe with sample data from cufflinks.

Plotly stacked bar chart:

enter image description here

Reproducible snippet:

# imports
import plotly
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import pandas as pd
import numpy as np

# setup
init_notebook_mode(connected=True)
np.random.seed(123)
cf.set_config_file(theme='pearl')

# qtconsole for debugging
#%qtconsole --style vim #src# https://qtconsole.readthedocs.io/en/stable/

# Random data using cufflinks
df = cf.datagen.lines()
df = df[['UUN.XY', 'MJF.XV', 'XBB.AO']].head(50)
df=np.abs(df)

# make figure
fig = df.iplot(asFigure=True, kind='bar', barmode = 'stack',
               xTitle='Dates',yTitle='Returns',title='Returns')

# plot figure
iplot(fig)

Edit - Test with negative values


Here's the same thing run without the line df=np.abs(df)

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

ValueError: All arguments should have the same length. The length of argument y is 3, whereas the length of previous arguments ['x'] is 7
2

It is now possible to make stacked bar plot directly with pandas by using plotly as plotting backend.

pd.options.plotting.backend = 'plotly'
fig = df.plot(kind='bar')

Comments

-3

Pandas DataFrame's plot can also be used here.

df.plot(kind='bar', stacked=True)

Also, here is a similar thread.

1 Comment

this isn't a plotly chart. Its need to be plotly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.