2015年11月25日水曜日

開発環境

  • 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 #1: How Does the Temperature Vary During the Day(No. 793)を解いてみる。

#1: How Does the Temperature Vary During the Day(No. 1438)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt

temperatures_newyork = [5, 4, 6, 9, 10, 9, 9, 9]
temperatures_washington = [2, 2, 7, 12, 13, 9, 7, 5]
times = (['{0}:00 AM'.format(x) for x in range(4, 13, 3)] +
         ['{0}:00 PM'.format(x) for x in range(13, 25, 3)] +
         ['1:00 AM'])

def draw_graph():
    xtimes = list(range(len(times)))
    plt.plot(xtimes, temperatures_newyork,
             xtimes, temperatures_washington,
             marker="o")
    plt.legend(['New York City', 'Washington, DC'])
    plt.title('Weather')
    plt.xlabel('the time of day')
    plt.ylabel('the temperature')    
    plt.axis(ymin=0)
    plt.xticks(xtimes, times)
    plt.savefig('weather.svg')
    plt.show()
    
if __name__ == '__main__':
    draw_graph()

入出力結果(Terminal, IPython)

0 コメント:

コメントを投稿