零散堆积的条形图熊猫数据框

时间:2019-03-13 09:28:52

标签: python pandas plotly

我有一个具有不同列数的数据框。具有正值和负值。是否可以使用数据框中的所有这些列制作堆叠的条形图?

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

2 个答案:

答案 0 :(得分:1)

这是在Jupyter Notebook中使用可规划的脱机堆积的条形图。只要确保您已安装imports下列出的软件包即可。 df = cf.datagen.lines()用袖扣中的示例数据生成熊猫数据框。

堆积的条形图:

enter image description here

可复制的代码段:

# 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)

编辑-使用负值进行测试


这里没有行df=np.abs(df)

enter image description here

答案 1 :(得分:0)

Pandas DataFrame的plot也可以在这里使用。

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

此外,here是一个类似的线程。

相关问题