2016年2月18日木曜日

開発環境

  • 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 5.(Playing with Sets and Probability)、Programming Challenges #1: Using Venn Diagrams to Visualize Relationships Between Sets(No. 3565)を解いてみる。

#4: Using Venn Diagrams to Visualize Relationships Between Sets(No. 3565)

コード(Emacs)

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

import sys
import sympy
from matplotlib_venn import venn2
import matplotlib.pyplot as plt
import csv


def draw_venn(sets, labels=None):
    if labels:
        venn2(subsets=sets, set_labels=labels)
    else:
        venn2(subsets=sets)
    plt.savefig('football_and_others.svg')
    plt.show()


def read_csv(filename):
    football = []
    others = []
    with open(filename) as f:
        reader = csv.reader(f)
        next(reader)
        for row in reader:
            if row[1] == '1':
                football.append(row[0])
            if row[2] == '1':
                others.append(row[0])
    return football, others

if __name__ == '__main__':
    football, others = read_csv(sys.argv[1])
    football = sympy.FiniteSet(*football)
    others = sympy.FiniteSet(*others)
    draw_venn([football, others], labels=('Football', 'Others'))

入出力結果(Terminal, IPython)

$ ./sample1.py
$

0 コメント:

コメントを投稿

Comments on Google+: