python堆叠面积图_python – pandas DataFrame的100%面积图

该方法与

the other SO answer基本相同;将每行除以行的总和:

df = df.divide(df.sum(axis=1), axis=0)

然后你可以照常调用df.plot(kind =’area’,stacked = True,…).

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

np.random.seed(2015)

y = np.random.randint(5, 50, (10,3))

x = np.arange(10)

df = pd.DataFrame(y, index=x)

df = df.divide(df.sum(axis=1), axis=0)

ax = df.plot(kind='area', stacked=True, title='100 % stacked area chart')

ax.set_ylabel('Percent (%)')

ax.margins(0, 0) # Set margins to avoid "whitespace"

plt.show()

产量

你可能感兴趣的:(python堆叠面积图)