【Python】Matplotlib ~ 基本編 / 棒グラフ ~

■ はじめに

https://dk521123.hatenablog.com/entry/2020/03/01/000000

の続き。

今回は、Python / Matplotlib (マット・プロット・リブ)で、
棒グラフや積み上げ棒グラフを表示させる

目次

【1】棒グラフ
 例1:Hello World
 例2:複数の棒グラフを横に並べる
【2】積み上げ棒グラフ
 例1:Hello World
【3】Tips
 1)横向きにしたい
 2)グラフタイトルを付ける
 3)凡例を付ける
 4)X軸・Y軸のラベルを付ける

【1】棒グラフ

* bar() を使う

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html

matplotlib.pyplot.bar(
  x, height, width=0.8, bottom=None, *,
  align='center', data=None, **kwargs)

例1:Hello World

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5])
y = np.array([100, 200, 300, 400, 500])

# 棒グラフ
plt.bar(x, y)
# 表示
plt.show()

例2:複数の棒グラフを横に並べる

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5, 6])
y1 = np.array([100, 200, 300, 400, 500, 600])
y2 = np.array([90, 70, 10, 30, 50, 0])
x_labels = ['A', 'B', 'C', 'D', 'E', 'F']

# 棒グラフ
target_width = 0.3
bar1 = plt.bar(x, y1, width=target_width, color="red")
bar2 = plt.bar(x + target_width, y2, width=target_width, color="blue")
plt.xticks(x + target_width/2, x_labels)

# 凡例を付ける
plt.legend((bar1[0], bar2[0]), ("Graph-bar1", "Graph-bar2"))

# 表示
plt.show()

別解

* 以下のサイトなどを参照

https://www.yutaka-note.com/entry/matplotlib_bar

【2】積み上げ棒グラフ

* bar() の bottomを指定する

例1:Hello World

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5])
y1 = np.array([100, 200, 300, 400, 500])
y2 = np.array([90, 70, 10, 30, 50])

# 棒グラフ
plt.bar(x, y1, color="red")
plt.bar(x, y2, bottom=y1, color="blue") # ★ここに注目
# 表示
plt.show()

【3】Tips

1)横向きにしたい

* barh() を使う

# 棒グラフ(横向き)
plt.barh(x, y)

2)グラフタイトルを付ける

* plt.title() を使う

# グラフタイトルを付ける
plt.title("Title for Hello world!")

3)凡例を付ける

* legend() を使って、bar のインスタンスを指定

# 棒グラフ
bar1 = plt.bar(x, y1, color="red")
bar2 = plt.bar(x, y2, bottom=y1, color="blue")
# 凡例を付ける
plt.legend((bar1[0], bar2[0]), ("Graph-bar1", "Graph-bar2"))

4)X軸・Y軸のラベルを付ける

* plt.subplots() と ax.set_xlabel/ax.set_ylable() を使う

サンプル

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5])
y1 = np.array([100, 200, 300, 400, 500])
y2 = np.array([90, 70, 10, 30, 50])

# https://www.yutaka-note.com/entry/matplotlib_subplots
# Figure(fig):描画領域全体
# Axes(ax):一つ一つのプロットを描く領域
fig, ax = plt.subplots()
# 複数
# fig, axes = plt.subplots(2, 3, tight_layout=True)

# 棒グラフ
ax.bar(x, y1, color="red")
ax.bar(x, y2, bottom=y1, color="blue")

# x 軸のラベルを設定
ax.set_xlabel("x axis")
# y 軸のラベルを設定
ax.set_ylabel("y axis")

# 表示
plt.show()

参考文献

https://pythondatascience.plavox.info/matplotlib/%e6%a3%92%e3%82%b0%e3%83%a9%e3%83%95

関連記事

Matplotlib ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/03/01/000000
Matplotlib ~ 基本編 / 折れ線 ~
https://dk521123.hatenablog.com/entry/2023/09/17/003431
Matplotlib ~ あれこれ編 ~
https://dk521123.hatenablog.com/entry/2023/09/14/230848
TensorFlow ~ 環境構築 / Windows 編 ~
https://dk521123.hatenablog.com/entry/2018/02/17/102927
scikit-learn ~ 機械学習用ライブラリ・基本編 ~
https://dk521123.hatenablog.com/entry/2020/03/08/113356
NumPy ~ 数値計算ライブラリ ~
https://dk521123.hatenablog.com/entry/2018/03/28/224532
Pandas ~ データ解析支援ライブラリ ~
https://dk521123.hatenablog.com/entry/2019/10/22/014957
Pandas ~ 基本編 / CSV編 ~
https://dk521123.hatenablog.com/entry/2020/11/17/000000
Python ~ 基本編 / CSV
https://dk521123.hatenablog.com/entry/2019/11/07/214108
Python ~ 基本編 / 文字列 ~
https://dk521123.hatenablog.com/entry/2019/10/12/075251
Python ~ 基本編 / 文字列比較 ~
https://dk521123.hatenablog.com/entry/2021/07/29/000000