開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More! (Amit Saha (著)、No Starch Press)のChapter 2.(Visualizing Data with Graphs)、Programming Challenges #4: Visualizing Your Expenses(No. 1472)を解いてみる。
#4: Visualizing Your Expenses(No. 1438)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
def create_bar_char(data, labels):
plt.title('Weekly expenditures')
num_bars = len(data)
positions = range(1, num_bars + 1)
plt.barh(positions, data, align='center')
plt.yticks(positions, labels)
plt.xlabel('Amount')
plt.ylabel('Categories')
plt.grid()
plt.savefig('weekly_expenditures.svg')
plt.show()
if __name__ == '__main__':
try:
categories = []
expenditures = []
num = int(input('Enter the number of categories: '))
for _ in range(num):
category = input('Enter category: ')
expenditure = int(input('Expenditure; '))
categories.append(category)
expenditures.append(expenditure)
print()
except Exception as err:
print(err)
else:
create_bar_char(expenditures, categories)
入出力結果(Terminal, IPython)
$ ./sample4.py Enter the number of categories: 4 Enter category: Food Expenditure; 70 Enter category: Transportation Expenditure; 35 Enter category: Entertainment Expenditure; 30 Enter category: Phone/Internet Expenditure; 30 $
0 コメント:
コメントを投稿
Comments on Google+: