Skip to content

Instantly share code, notes, and snippets.

@nishidy
Last active March 8, 2016 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishidy/023aafa8c2ab956087cf to your computer and use it in GitHub Desktop.
Save nishidy/023aafa8c2ab956087cf to your computer and use it in GitHub Desktop.
Python pandas chart test script
# coding: utf-8
import csv
import urllib3
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import pandas as pd
import numpy as np
import datetime as dt
plt.style.use('ggplot')
fp = FontProperties(fname='C:\Windows\Fonts\HGRME.TTC')
#font = {'family' : 'meiryo'}
#matplotlib.rc('font', **font)
http = urllib3.ProxyManager( proxy_url = 'http://proxy.xxx.com:8080' )
url = 'https://vincentarelbundock.github.io/Rdatasets/csv/robustbase/ambientNOxCH.csv'
response = http.request("GET", url)
cr = csv.reader(response.data.decode('utf-8').splitlines())
header = next(cr)
# ['', 'date', 'ad', 'ba', 'ef', 'la', 'lu', 're', 'ri', 'se', 'si', 'st', 'su', 'sz', 'zg']
list_data = [row for row in cr]
# [['1', '2004-01-01', '11.9800087363542', '14.6621417249783', '17.3327675', ...
df_sample =pd.DataFrame(list_data, columns=header)
df = df_sample.ix[: , 2:].replace('NA', 0).astype(np.float)
df_month = df.copy()
df.index = [dt.datetime.strptime(x, '%Y-%m-%d') for x in df_sample.date]
df_month['month'] = [ "-".join(x.split("-")[:2]) for x in df_sample.date]
df_monthly = df_month.groupby('month').sum()
df_monthly.plot.bar(y=['ad', 'ba', 'se'], alpha=0.6, figsize=(12,3))
plt.title('棒グラフ', size=16, fontproperties=fp)
plt.savefig("figure1.png")
plt.close()
df_monthly.plot.bar(y=['ad', 'ba', 'se'], alpha=0.6, figsize=(12,3), stacked=True, cmap='Blues')
plt.title('積み上げ棒グラフ', size=16, fontproperties=fp)
plt.savefig("figure2.png")
plt.close()
df.plot( y=['ad', 'ba', 'ef'], figsize=(16,4), alpha=0.5)
plt.title('時系列グラフ', size=16, fontproperties=fp)
plt.savefig("figure3.png")
plt.close()
df.plot( y=['ad', 'ba', 'ef'], bins=50, alpha=0.5, figsize=(16,4), kind='hist')
plt.title('ヒストグラム', size=16, fontproperties=fp)
plt.savefig("figure4.png")
fig, axes = plt.subplots(2, 2, figsize=(14, 10), sharey=True)
# 1.普通のプロット
df[0:150].plot(x='ad', y='ba', kind='scatter', ax=axes.flatten()[0])
# 2.離散値を色で表す
df[0:150].plot(x='ad', y='ba', kind='scatter', c='ef', cmap='Blues', ax=axes.flatten()[1])
# 3.離散値をバブルサイズで表す(ちょっと数は減らしてる)
df[0:150].plot.scatter(x='ad', y='ba', s=(df['su']-20)*3, ax=axes.flatten()[2])
# 4.両方同時に使う
df[0:150].plot.scatter(x='ad', y='ba', s=(df['su']-20)*3, c='ef', ax=axes.flatten()[3])
fig.savefig("figure5.png")
df[0:150].plot(x='ad', y='ba',kind='hexbin', gridsize=20)
plt.title('Hexabin', size=16)
plt.savefig("figure6.png")
plt.close()
df_monthly.plot( kind='pie', y = 'se')
plt.title('円グラフ', size=16, fontproperties=fp)
plt.savefig("figure7.png")
plt.close()
df_monthly.plot(y=['se', 'ef'], kind='area', stacked=True, alpha=0.4)
plt.title('エリアチャート', size=16, fontproperties=fp)
plt.savefig("figure8.png")
plt.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment