18
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pyplot(1) 棒グラフ

Last updated at Posted at 2019-08-04

棒グラフまとめ

縦棒グラフ

基本形

必要なライブラリをインポートします。

import numpy as np
import matplotlib.pyplot as plt

データを準備します。データはlistでもarrayでも大丈夫でした。

means = [20, 34, 30, 35, 27]

各棒の横幅を設定します。

x = np.arange(len(labels))
width = 0.35

グラフを表示します。

plt.bar(x, men_means, width)
plt.show()

まとめると以下のようになります。

import numpy as np
import matplotlib.pyplot as plt

means = [20, 34, 30, 35, 27]

x = np.arange(len(labels))
width = 0.35

plt.bar(x, men_means, width)
plt.show()

image_01.png

タイトル・ラベル追加

plt.xlabelでx軸方向のラベル、plt.ylabelでy軸方向のラベル、plt.tileでタイトルをそれぞれ設定できます。

plt.xlabel('Group')
plt.ylabel('Scores')
plt.title('Scores by group')

image_02.png

棒ごとのラベル追加

import numpy as np
import matplotlib.pyplot as plt

labels = ['label1', 'label2', 'label3', 'label4', 'label5']
means = [20, 34, 30, 35, 27]

x = np.arange(len(labels))
width = 0.35

fig, ax = plt.subplots()

rect = ax.bar(x, men_means, width)
ax.set_xticks(x)
ax.set_xticklabels(labels)

plt.show()

ax.set_xticks(x)を省略すると、棒ごとのラベルの位置がずれるので注意。

bar_02.png

棒の上に値を表示

import numpy as np
import matplotlib.pyplot as plt

labels = ['label1', 'label2', 'label3', 'label4', 'label5']
means = [20, 34, 30, 35, 27]

x = np.arange(len(labels))
width = 0.35

fig, ax = plt.subplots()

rect = ax.bar(x, men_means, width)
ax.set_xticks(x)
ax.set_xticklabels(labels)

def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                   xy=(rect.get_x() + rect.get_width() / 2, height),
                   xytext=(0, 3),
                   textcoords="offset points",
                   ha='center', va='bottom')
autolabel(rect)      
plt.show()

bar_01.png

regend

import numpy as np
import matplotlib.pyplot as plt

labels = ['label1', 'label2', 'label3', 'label4', 'label5']
means = [20, 34, 30, 35, 27]

x = np.arange(len(labels))
width = 0.35

fig, ax = plt.subplots()

rect = ax.bar(x, men_means, width, label='Men')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                   xy=(rect.get_x() + rect.get_width() / 2, height),
                   xytext=(0, 3),
                   textcoords="offset points",
                   ha='center', va='bottom')
autolabel(rect)      

plt.show()

bar_03.png

左上に表示させたい場合は以下のようにします。

ax.legend(loc='upper left')

bar_04.png

横並び

1種の場合とほとんど同様なのですが、表示させる位置をずらしてあげることで2つ並んでいるように見せることができます。

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]

x = np.arange(len(labels))
width = 0.35

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, men_means, width, label='Men')
rects2 = ax.bar(x + width/2, women_means, width, label='Women')

ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xlabel('test')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()

def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                   xy=(rect.get_x() + rect.get_width() / 2, height),
                   xytext=(0, 3),
                   textcoords="offset points",
                   ha='center', va='bottom')
        
autolabel(rects1)
autolabel(rects2)

fig.tight_layout()

plt.show()

image_04.png

積み上げ縦棒グラフ

import numpy as np
import matplotlib.pyplot as plt

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
menStd = [2, 3, 4, 1, 2]
womenStd = [3, 5, 2, 3, 3]

x = np.arange(len(labels))
width = 0.35

p1 = plt.bar(x, men_means, width, yerr=menStd)
p2 = plt.bar(x, women_means, width, bottom=men_means, yerr=womenStd)
plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(x, labels)
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))

plt.show()

image_03.png

横棒グラフ

barではなくbarhを使えば、横方向の棒グラフを描画できます。
縦方向の場合と比較して、ラベルなどを設定する軸が変わることに注意が必要です。

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)

plt.rcdefaults()
fig, ax = plt.subplots()

people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))

ax.barh(y_pos, performance, xerr=error, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis()
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')

image_05.png

18
23
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?