Skip to content

Instantly share code, notes, and snippets.

@seafooler
Created November 12, 2019 17:33
Show Gist options
  • Save seafooler/88ff3a0353ace8eb1e139099ce676e4a to your computer and use it in GitHub Desktop.
Save seafooler/88ff3a0353ace8eb1e139099ce676e4a to your computer and use it in GitHub Desktop.
Example for drawing stacked bars plot in python
import pandas as pd
import matplotlib.pyplot as plt
xticks = ['0~5', '5~10', '10~15', '15~20', '20~25', '25~30', '30~35', '35~40', '40~45', '45~49']
data1 = [39266.6454,39655.67939,39961.77618,39421.58352,45809.54727,45992.33723,46111.81729,42279.75517,47363.08822,39355.46721]
data2 = [744.3184969,580.8364771,608.6965388,599.6553536,556.4223923,386.1615846,452.205138,389.9741167,343.7995401,306.9898914]
data3 = [10016.60611,9788.374129,9461.967283,9994.091129,3664.340342,3645.11118,3461.977574,7361.090709,2341.232241,1987.660203]
index = pd.Index(xticks, name='test')
data = {
'data1': data1,
'data2': data2,
'data3': data3
}
df = pd.DataFrame(data, index=index)
# ax = df.plot(kind='bar', stacked=True, figsize=(18.5, 10.5))
ax = df.plot(kind='bar', stacked=True)
ax.set_ylabel('Time (s)', fontsize = 'large')
ax.set_xlabel('Period (10,000s)', fontsize = 'large')
ax.legend(loc=3, fontsize=10)
plt.tight_layout()
plt.savefig('test.eps')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment