2015-04-29 22 views

Trả lời

8

Phương pháp cơ bản giống như trong the other SO answer; chia mỗi hàng theo tổng của hàng:

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

Sau đó, bạn có thể gọi df.plot(kind='area', stacked=True, ...) như bình thường.


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

mang

enter image description here

+1

Một khả năng khác là 'pandas.DataFrame.plot.area() ' – Dror

Các vấn đề liên quan