![見出し画像](https://assets.st-note.com/production/uploads/images/66807143/rectangle_large_type_2_47342464adf2dbacb7517757b5a110ad.png?width=800)
[第2回] matplotlibを使ってグラフを作ってみる
jupyter notebookとpythonを利用した分析講座となっております。
データフレーム
・jupyter notebookに下記コードを読み込ませてデータフレームを作成する
# グラフ化に必要なものの準備
import matplotlib as mpl
import matplotlib.pyplot as plt
import japanize_matplotlib
# データの扱いに必要なライブラリ
import pandas as pd
import numpy as np
import datetime as dt
# グラフのスタイル設定
plt.style.use('ggplot')
font = {'family' : 'TakaoPGothic'}
# CSV読込
url = 'https://vincentarelbundock.github.io/Rdatasets/csv/robustbase/ambientNOxCH.csv'
df_sample = pd.read_csv(url, parse_dates=True, index_col=1)
# dfの準備
df = df_sample.iloc[:, 1:]
# df_monthlyの準備
df_monthly = df.copy()
df_monthly.index = df_monthly.index.map(lambda x: x.month)
df_monthly = df_monthly.groupby(level=0).sum()
![画像1](https://assets.st-note.com/production/uploads/images/66321377/picture_pc_b680273ac2b6fb060c1b2b62dd185b05.png?width=800)
グラフの作成
jupyter notebookにそれぞれ下記のコマンドを入力してグラフを作成する
bar chart
![画像2](https://assets.st-note.com/production/uploads/images/66321401/picture_pc_9641490e86db1ed9c78e9b3a3c57901a.png?width=800)
df_monthly.plot.bar(y=['ad','ba','se'], alpha=0.8, figsize=(12,3), cmap='Paired')
bar chart 積上
![画像3](https://assets.st-note.com/production/uploads/images/66321416/picture_pc_0f26401d9d5f478fbf8f78131d597a76.png?width=800)
df_monthly.plot.bar(y=['ad','ba','se'], alpha=0.8, figsize=(12,3), stacked=True, cmap='Purples')
Line Chart
![画像4](https://assets.st-note.com/production/uploads/images/66321432/picture_pc_d9004e00bbef7ac3d9907ab1c502b4e8.png?width=800)
df_monthly.plot.line(y=['se','ef'], marker='o')
pie chart
![画像5](https://assets.st-note.com/production/uploads/images/66321466/picture_pc_339079926455d5394392d3b03d1204dc.png?width=800)
df_monthly.plot(kind='pie', y='se', figsize=(5,5), cmap='tab20', autopct='%.2f')
ヒストグラム
![画像6](https://assets.st-note.com/production/uploads/images/66321481/picture_pc_a754a52156cfe6b49fca4dd17080f110.png?width=800)
ax= df.plot(y=['ad','ba','ef'], bins=50, alpha=0.5, figsize=(16,4), kind='hist' cmap='Set1')
散布図
![画像7](https://assets.st-note.com/production/uploads/images/66321503/picture_pc_9aefaaf7332ed68da84354915a658851.png?width=800)
エリアチャート
![画像8](https://assets.st-note.com/production/uploads/images/66321518/picture_pc_7b0a0413d8486f86586ab6161335ef7c.png?width=800)
df_monthly.plot(kind='area', y=['se','ef'], stacked=True, alpha=0.4)
はこひげ
![画像9](https://assets.st-note.com/production/uploads/images/66321538/picture_pc_70eefa869ad0f5101f6c32b8c94f2504.png?width=800)
df_monthly.plot.box()
次回講座
この記事が気に入ったらサポートをしてみませんか?