Created
July 31, 2018 07:06
-
-
Save sweemeng/9a19623ae591eed944c2efd7037802c0 to your computer and use it in GitHub Desktop.
Generate stacked chart from open data from http://www.data.gov.my/data/ms_MY/dataset/employed-persons-by-state-and-educational-attainment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import os | |
def main(): | |
df = pd.read_csv("./bptms_Employed_persons_by_educational_attainment_state-1.csv") | |
if not os.path.exists("image_dir"): | |
os.makedirs("image_dir") | |
for s in df["State/Country"].unique(): | |
plot = draw_chart(df, s) | |
figure = plot.get_figure() | |
figure.savefig("image_dir/" + s+".png") | |
def draw_chart(df, state): | |
df_state = df[df["State/Country"] == state] | |
df_year = df_state.pivot(index="Year", columns="Educational attainment", values=" Employed Person ('000) ") | |
df_year = df_year.convert_objects(convert_numeric=True) | |
title = "{state} employee in (,000)".format(state=state) | |
plot = df_year.drop(["Total"],axis=1).plot.area(title=title, ) | |
return plot | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment