ぱたへね

はてなダイアリーはrustの色分けができないのでこっちに来た

グラフタイプ

Beginning Python Visualizationの6章から。
GDPのデータを使って、様々なグラフを書いてみます。

テキストに日本語を入れるには、font_managerを使います。

import matplotlib.font_manager

prop = matplotlib.font_manager.FontProperties(fname=r'c:\windows\fonts\msgothic.ttc')
xticks(arange(N), labels, fontproperties = prop)


他のグラフはmatplotlibのギャラリーを見るのが良いです。
http://matplotlib.sourceforge.net/gallery.html

サンプル画像

ソースファイル

# -*- coding: utf-8 -*-
from pylab import *
import matplotlib.font_manager

prop = matplotlib.font_manager.FontProperties(fname=r'c:\windows\fonts\msgothic.ttc')

# http://www.yenjie.net/factbook/rankorder/2001rank.txt から
countries = [' United States ', ' European Union ', ' China ', ' Japan ', ' India ', ' Germany ', ' United Kingdom ', ' France ', ' Italy ', ' Brazil ', ' Russia ', ' Canada ', ' Mexico ', ' Spain ', ' Korea South ', ' Indonesia ', ' Australia ', ' Taiwan ', ' Thailand ', ' Iran ', ' Turkey ', ' South Africa ', ' Argentina ', ' Netherlands ', ' Poland ', ' Philippines ', ' Pakistan ', ' Egypt ', ' Belgium ', ' Saudi Arabia ', ' Ukraine ', ' Colombia ', ' Bangladesh ', ' Austria ', ' Sweden ', ' Switzerland ', ' Hong Kong ', ' Malaysia ', ' Vietnam ', ' Greece ', ' Algeria ', ' Portugal ', ' Norway ', ' Denmark ', ' Czech Republic ', ' Romania ', ' Chile ', ' Peru ', ' Finland ', ' Hungary ', ' Venezuela ', ' Morocco ', ' Israel ', ' Ireland ', ' Nigeria ', ' Singapore ', ' Kazakhstan ', ' New Zealand ', ' Sri Lanka ', ' Slovakia ', ' Sudan ', ' Burma ', ' Tunisia ', ' Belarus ', ' Puerto Rico ', ' United Arab Emirates ', ' Bulgaria ', ' Syria ', ' Guatemala ', ' Dominican Republic ', ' Ethiopia ', ' Iraq ', ' Croatia ', ' Ecuador ', ' Uruguay ', ' Ghana ', ' Kuwait ', ' Uzbekistan ', ' Lithuania ', ' Congo Democratic Republic of the ', ' Korea North ', ' Nepal ', ' Slovenia ', ' Uganda ', ' Oman ', ' Costa Rica ', ' Libya ', ' Kenya ', ' Cuba ', ' El Salvador ', ' Cameroon ', ' Azerbaijan ', ' Paraguay ', ' Turkmenistan ', ' Luxembourg ', ' Cambodia ', ' Latvia ', ' Serbia and Montenegro ', ' Bosnia and Herzegovina ', ' Jordan ', " Cote d'Ivoire ", ' Zimbabwe ', ' Tanzania ', ' Mozambique ', ' Angola ', ' Bolivia ', ' Afghanistan ', ' Panama ', ' Guinea ', ' Qatar ', ' Estonia ', ' Lebanon ', ' Honduras ', ' Senegal ', ' Albania ', ' Yemen ', ' Burkina Faso ', ' Cyprus ', ' Mauritius ', ' Chad ', ' Botswana ', ' Namibia ', ' Madagascar ', ' Georgia ', ' Macedonia ', ' Armenia ', ' Bahrain ', ' Nicaragua ', ' Haiti ', ' Papua New Guinea ', ' Trinidad and Tobago ', ' Laos ', ' Jamaica ', ' Mali ', ' Rwanda ', ' Niger ', ' Zambia ', ' Iceland ', ' Macau ', ' Togo ', ' Moldova ', ' Kyrgyzstan ', ' Benin ', ' Gabon ', ' Tajikistan ', ' Malawi ', ' Malta ', ' Brunei ', ' Martinique ', ' Swaziland ', ' Lesotho ', ' Mauritania ', ' Mongolia ', ' Bahamas The ', ' Fiji ', ' Somalia ', ' French Polynesia ', ' Reunion ', ' Barbados ', ' Cyprus ', ' Central African Republic ', ' Eritrea ', ' Burundi ', ' Jersey ', ' Guadeloupe ', ' Sierra Leone ', ' Guam ', ' New Caledonia ', ' Liberia ', ' Bhutan ', ' Guyana ', ' Gambia The ', ' Guernsey ', ' Virgin Islands ', ' British Virgin Islands ', ' Netherlands Antilles ', ' Bermuda ', ' Congo Republic of the ', ' Man Isle of ', ' Aruba ', ' Andorra ', ' Suriname ', ' West Bank ', ' Belize ', ' French Guiana ', ' Cayman Islands ', ' Equatorial Guinea ', ' Maldives ', ' Greenland ', ' Guinea-Bissau ', ' Faroe Islands ', ' Samoa ', ' San Marino ', ' Northern Mariana Islands ', ' Monaco ', ' Saint Lucia ', ' Liechtenstein ', ' Solomon Islands ', ' Gibraltar ', ' Gaza Strip ', ' Antigua and Barbuda ', ' Seychelles ', ' Djibouti ', ' Cape Verde ', ' Vanuatu ', ' American Samoa ', ' Mayotte ', ' Comoros ', ' Grenada ', ' Dominica ', ' East Timor ', ' Saint Vincent and the Grenadines ', ' Saint Kitts and Nevis ', ' Micronesia Federated States of ', ' Tonga ', ' Turks and Caicos Islands ', ' Sao Tome and Principe ', ' Palau ', ' Marshall Islands ', ' Anguilla ', ' Cook Islands ', ' Kiribati ', ' Falkland Islands (Islas Malvinas) ', ' Nauru ', ' Wallis and Futuna ', ' Saint Pierre and Miquelon ', ' Montserrat ', ' Saint Helena ', ' Tuvalu ', ' Niue ', ' Tokelau ']

gdp_str = [ ' 11750000000000', ' 11650000000000', ' 7262000000000', ' 3745000000000', ' 3319000000000', ' 2362000000000', ' 1782000000000', ' 1737000000000', ' 1609000000000', ' 1492000000000', ' 1408000000000', ' 1023000000000', ' 1006000000000', ' 937600000000', ' 925100000000', ' 827400000000', ' 611700000000', ' 576200000000', ' 524800000000', ' 516700000000', ' 508700000000', ' 491400000000', ' 483500000000', ' 481100000000', ' 463000000000', ' 430600000000', ' 347300000000', ' 316300000000', ' 316200000000', ' 310200000000', ' 299100000000', ' 281100000000', ' 275700000000', ' 255900000000', ' 255400000000', ' 251900000000', ' 234500000000', ' 229300000000', ' 227200000000', ' 226400000000', ' 212300000000', ' 188700000000', ' 183000000000', ' 174400000000', ' 172200000000', ' 171500000000', ' 169100000000', ' 155300000000', ' 151200000000', ' 149300000000', ' 145200000000', ' 134600000000', ' 129000000000', ' 126400000000', ' 125700000000', ' 120900000000', ' 118400000000', ' 92510000000', ' 80580000000', ' 78890000000', ' 76190000000', ' 74300000000', ' 70880000000', ' 70500000000', ' 68950000000', ' 63670000000', ' 61630000000', ' 60440000000', ' 59470000000', ' 55680000000', ' 54890000000', ' 54400000000', ' 50330000000', ' 49510000000', ' 49270000000', ' 48270000000', ' 48000000000', ' 47590000000', ' 45230000000', ' 42740000000', ' 40000000000', ' 39530000000', ' 39410000000', ' 39390000000', ' 38090000000', ' 37970000000', ' 37480000000', ' 34680000000', ' 33920000000', ' 32350000000', ' 30170000000', ' 30010000000', ' 29930000000', ' 27600000000', ' 27270000000', ' 26990000000', ' 26530000000', ' 26270000000', ' 26210000000', ' 25500000000', ' 24780000000', ' 24370000000', ' 23710000000', ' 23380000000', ' 23170000000', ' 22330000000', ' 21500000000', ' 20570000000', ' 19500000000', ' 19490000000', ' 19230000000', ' 18830000000', ' 18790000000', ' 18360000000', ' 17460000000', ' 16250000000', ' 15740000000', ' 15710000000', ' 15680000000', ' 15660000000', ' 15050000000', ' 14760000000', ' 14560000000', ' 14450000000', ' 14400000000', ' 13650000000', ' 13010000000', ' 12340000000', ' 12050000000', ' 11990000000', ' 11480000000', ' 11280000000', ' 11130000000', ' 11000000000', ' 10430000000', ' 9716000000', ' 9409000000', ' 9373000000', ' 9100000000', ' 8684000000', ' 8581000000', ' 8495000000', ' 8338000000', ' 7966000000', ' 7950000000', ' 7410000000', ' 7223000000', ' 6842000000', ' 6117000000', ' 6018000000', ' 5892000000', ' 5534000000', ' 5332000000', ' 5295000000', ' 5173000000', ' 4597000000', ' 4580000000', ' 4570000000', ' 4569000000', ' 4540000000', ' 4248000000', ' 4154000000', ' 4001000000', ' 3600000000', ' 3513000000', ' 3335000000', ' 3200000000', ' 3158000000', ' 2903000000', ' 2900000000', ' 2899000000', ' 2799000000', ' 2590000000', ' 2500000000', ' 2498000000', ' 2450000000', ' 2330000000', ' 2324000000', ' 2113000000', ' 1940000000', ' 1900000000', ' 1885000000', ' 1800000000', ' 1778000000', ' 1551000000', ' 1391000000', ' 1270000000', ' 1250000000', ' 1100000000', ' 1008000000', ' 1000000000', ' 1000000000', ' 940000000', ' 900000000', ' 870000000', ' 866000000', ' 825000000', ' 800000000', ' 769000000', ' 768000000', ' 750000000', ' 626000000', ' 619000000', ' 600000000', ' 580000000', ' 500000000', ' 466800000', ' 441000000', ' 440000000', ' 384000000', ' 370000000', ' 342000000', ' 339000000', ' 277000000', ' 244000000', ' 216000000', ' 214000000', ' 174000000', ' 115000000', ' 112000000', ' 105000000', ' 79000000', ' 75000000', ' 60000000', ' 60000000', ' 48300000', ' 29000000', ' 18000000', ' 12200000', ' 7600000', ' 1500000']

gdps = map((lambda x: float(x)/1e9), gdp_str)

def bar_chart():
    gdp = (13780, 7099, 4772, 2966, 2807)
    labels = (u'アメリカ', u'中国', u'日本', u'インド', u'ドイツ')
    N = len(gdp)
    bar(arange(N), gdp, align= 'center')
    xticks(arange(N), labels, fontproperties = prop)
    for i ,val in enumerate(gdp):
        text(i, val/2, str(val), va = 'center', ha = 'center', color = 'yellow')
    ylabel('$ (Billions)')
    title(u'GDPランク', fontproperties = prop)
    savefig('bar.png')
    show()

def histgrams():
    N = len(countries)
    B = 1000
    prob, bins, patches = hist(gdps, arange(0, max(gdps)+B, B), align = 'left')
    
    for i, p in enumerate(prob):
        percent = float(p)/N*100
        if percent:
            percent_s = '%2.2f%%' % percent
            text(bins[i], p, percent_s,
                 rotation=45, va = 'bottom', ha = 'center')
    ylabel('Number of countries')
    xlabel('Income, billions of dollars')
    title('GDP histgram')
    
    xlim(-B/2, xlim()[1]-B/2)
    savefig('histgrams.png')
    show()

def pie_chart():
    N = 10
    pie(gdps[:N], labels=countries[:N], shadow=True)
    title('GDP ranks')
    savefig('pie.png')
    show()

def main():
    bar_chart()
    histgrams()
    pie_chart()

if __name__ == '__main__':
    figure()
    main()

bar.png
histgrams.png
pie.png