こんな人にオススメ
matplotlib.pyplot
、plt
で棒グラフを書くにはどうしたらいいんだ?
あと、どんな感じのグラフができるんだ?
ということで、今回はmatplotli.pyplot
、plt
で棒グラフを作成する。これまで多く取り上げてきたのは折れ線グラフとか散布図とか言われるようなグラフ。
作り方についてはそこまで複雑さはないけど、やはり線と面では使えるオプションが異なる、棒グラフが使えるようになれば降水量とかのグラフを表示できるようになる(多分)。
python環境は以下。
- Python 3.9.4
- numpy 1.20.3
- matplotlib 3.4.2
- pandas 1.2.4
下準備
import re import sys import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm import pandas as pd sys.path.append('../../') import plt_layout_template as template x = np.arange(5) print(x) # [0 1 2 3 4] # 今回使用する棒グラフのyの値 height1 = np.array([3, 1.5, 5, 1.2, 2.4]) height2 = np.array([2.4, 2.1, 4.2, 0.8, 4.1]) height3 = np.array([3.2, 4.8, 1.0, 0.2, 4.4]) height4 = np.array([1.0, 1.4, 2.6, 4.9, 4.8])
まずは本記事で作成するグラフの下準備としてのimport関連とデータ定義。今回は横軸には5データを使用し、縦軸のデータは合計で4種類使用することにした。縦軸のデータheight1
からheight4
の値は以下。
print(height1) # [3. 1.5 5. 1.2 2.4] print(height2) # [2.4 1. 4.2 0.8 1.1] print(height3) # [3.2 4.8 1. 0.2 4.4] print(height4) # [1. 1.4 2.6 4.9 4.8]
plt_layout_template
はこのコードのあるディレクトリから上に2ついったところに置いてあるので、sys.path.append('../../')
で2つ戻っている。plt_layout_template
については以下参照。
-
-
【pltテンプレート】matplotlib.pyplotのグラフ作成テンプレート
続きを見る
シンプルな棒グラフの作成
まずはシンプルな棒グラフ。棒グラフはax.bar
で作成可能で、引数のx
が横軸の配列、height
が縦軸の値を示す。必要ではないが、一応グリッドも表示している。コードは以下。
def bar_simple(): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') ax.bar(x=x, height=height1, label='height1',) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_simple.png') bar_simple()
オプションの設定
ax.bar
には数多くの引数が存在している。ここではその中から使いやすいものをピックアップして紹介する。
横軸ラベルを変更
横軸目盛に表示する値は変更することができる。変更はax.bar
の匹数tick_label
で可能で、None
を指定すると目盛が消えるようだ。
# 横軸ラベルの変更 def bar_tick_label(tick_label: tuple): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') ax.bar( x=x, height=height1, label='height1', tick_label=tick_label, # 横軸ラベル ) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_tick_label.png') bar_tick_label( tick_label=['A', 'bb', None, 1 == 2, 10] )
棒グラフに枠線を追加
棒グラフの枠線の色と太さは変更可能で、引数はそれぞれedgecolor
, linewidth
。上の画像では枠線の色を'deeppink'
、枠線の太さを3
とした。
# 棒グラフの枠線の色と太さ def bar_baredge(edgecolor: str, linewidth: int or float or tuple, name: str): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') ax.bar( x=x, height=height1, label='height1', edgecolor=edgecolor, # 棒グラフの枠線の色 linewidth=linewidth, # 棒グラフの枠線の太さ ) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig(f"bar_baredge_{name}.png") # 棒グラフの枠線の太さを一括変更 bar_baredge( edgecolor='deeppink', linewidth=3, name=3, )
また、色も太さも配列で指定することができる。以下の例では色はviolet系の色、太さは適当に選んだ配列を使って指定した。
# # 枠線の色と太さを別々に指定 color = ( 'violet', 'darkviolet', 'palevioletred', 'mediumvioletred', 'blueviolet', ) bar_baredge( edgecolor=color, linewidth=(10, 8, 5, 3, 1), name='each', )
棒グラフ間隔の変更
棒グラフ自体の太さも変更可能で、デフォルトでは0.8の太さのようだ。棒グラフ同士をくっつけるには、横軸の間隔と同じ値を引数width
で指定すればいい。ここでは横軸の間隔が1
なので、width=1
とするとくっつく。
# 棒グラフ自体の太さ def bar_width(width: int or float or tuple, name: str): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') ax.bar( x=x, height=height1, label='height1', width=width, # 棒グラフの太さ(xの間隔が1なので棒グラフはくっつく) ) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig(f"bar_width_{name}.png") # 全棒グラフの太さを一括変更 bar_width( width=1, name=1, )
また、棒グラフの太さも配列を指定することでバラバラにすることができる。
# tupleでそれぞれの棒グラフの太さを変更 bar_width( width=(1, 0.1, 1.5, 0.3, 0.8), name='each', )
棒グラフの色を変更
先ほどは枠線自体の色を変更したが、もちろん棒グラフ自体の色も変更可能。線グラフだと線の色しか変更できないが、棒グラフだと面なので、線の情報に加えて面の情報もいじることができる。
色を一括変更
棒グラフの色を変更するには引数としてcolor
を使用すればいい。上の例では'violet'
に設定している。
# 棒グラフの色(全棒グラフの色を変更) def bar_color(color: 'str'): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') ax.bar( x=x, height=height1, label='height1', color=color, # 棒グラフ色 ) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_color.png') bar_color( color='violet', )
色を個別変更
もちろん色も個別で設定可能。設定は案の定、配列。今回は'violet'
系で攻めようと思う。ただし、凡例に適用される色は初めの棒グラフの色のようだ。
# 棒グラフの色(それぞれの棒グラフの色を個別に変更) def bar_color_each(color: tuple, name: str): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') ax.bar( x=x, height=height1, label='height1', color=color, # 棒グラフ色 ) ax.legend() # 凡例は初めの棒グラフの色が反映 ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig(f"bar_color_each_{name}.png") color = ( 'violet', 'darkviolet', 'palevioletred', 'mediumvioletred', 'blueviolet', ) bar_color_each( color=color, name='violet', )
また、カラーマップからも作成することが可能。この時はカラーマップの配列を予め作成しないといけない。
# 連続色で色を作成 color = [] for num in range(len(x)): cmap = cm.jet(num / len(x)) color.append(cmap) print(color) # [(0.0, 0.0, 0.5, 1.0), (0.0, 0.3, 1.0, 1.0), (0.16129032258064513, 1.0, 0.8064516129032259, 1.0), (0.8064516129032256, 1.0, 0.16129032258064513, 1.0), (1.0, 0.40740740740740755, 0.0, 1.0)] bar_color_each( color=color, name='jet', )
色を一部変更
さっきの棒グラフの色変えだと、大量の棒グラフの中の一部の色だけを変更したいときも大変。イメージは以下。
color = [ 'violet', 'violet', 'violet', 'violet', 'violet', 'violet', 'violet', 'violet', 'violet', 'violet', 'violet', 'violet', 'violet', 'blueviolet', 'violet', 'violet', 'blueviolet', 'violet', 'violet', 'violet', 'violet', ]
もちろん予めlist
にデフォルトの色の配列を作って、それから変更するのもいいだろう。
color = ['violet'] * 21 color[-5] = 'blueviolet' color[13] = 'blueviolet'
しかし、デフォルトの棒グラフの色を使用しつつ一部の色だけを変更したい。デフォルトの色も調べたら出てくるけど面倒。そんな時は色をセットすればいい。
ax.bar
を変数に代入し、その変数の指定のインデックスの色を.set_color
で変更することで、デフォルトの色を保ったまま一部の色を変更することができる。
# 棒グラフの色(それぞれの棒グラフの色を個別に変更) def bar_color_index(**kwargs): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') bar_lst = ax.bar(x=x, height=height1, label='height1',) for index, color in kwargs.items(): bar_lst[int(index)].set_color(color) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_color_index.png') # kwargsのkeysは文字じゃないといけない かつ 直接入れられないので一旦dictにする kwargs = {'1': 'blueviolet', '3': 'palevioletred', } bar_color_index( **kwargs )
なお、ここでは一括管理ということでkwargs
引数で対応したが、もちろん引数を2つに増やすなど他の方法も可能。kwargs
引数にすることで、好きな数だけ指定することができる。
ここまでの設定オプション全部載せ棒グラフ
ここまでのグラフのオプションを全部盛り込んだグラフが上のグラフ。横軸は棒グラフのそれぞれの色を示しつつ、棒グラフ同士が離れないようにwidth
を1
とした。さらに境界線として黒色を選択。
# 上記オプション全部載せ def bar_option(color: tuple, edgecolor: str, linewidth: int or float, width: int or float): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') ax.bar( x=x, height=height1, label='height1', tick_label=color, edgecolor=edgecolor, linewidth=linewidth, width=width, color=color, ) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_option.png') color = ( 'violet', 'darkviolet', 'palevioletred', 'mediumvioletred', 'blueviolet', ) bar_option( color=color, edgecolor='black', linewidth=2, width=1, )
棒グラフの上に値を表示
棒グラフの上にテキストを入れるには複数種類の方法がある。簡単なものから難しいものまである。
ax.text
で表示
棒グラフとは別に、特立したテキストとして値を表示するにはax.text
を使用すればいい。しかし、ax.text
では配列で渡せないので、for
ループで1つずつテキストの表示をする。
# ax.textとサクッと値を表示 def bar_text(): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') ax.bar(x=x, height=height1, label='height1',) for xval, yval in zip(x, height1): ax.text(xval, yval, yval, ha='center', va='bottom') ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_text.png') bar_text()
ax.annotate
で表示(面倒)
ax.bar
の値の表示で調べると結構な確率でこのax.annotate
の記事が見つかる。出典元はReferenceなんだけど、このサイトだとmatplotlib
のバージョンが古いらしく
「You are reading an old version of the documentation (v3.1.1). For the latest version see https://matplotlib.org/stable/gallery/lines_bars_and_markers/barchart.html」
と言われる。まあできるからやってみる。
# referenceにある書き方(古め) # <https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/barchart.html> def bar_autolabel(): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') rects1 = ax.bar(x=x, height=height1, label='height1',) for rect in rects1: # 置きたいテキスト=棒グラフの高さ hght = rect.get_height() print(hght) # 1本目の棒グラフ: 3.0 # テキストを置く座標位置 xy = (rect.get_x() + rect.get_width() / 2, hght) print(xy) # 1本目の棒グラフ: (0.0, 3.0) ax.annotate( hght, xy=xy, # 矢印の終端 xytext=(0, 3), # ラベルの位置((0, 3)にする意味がよくわからん) textcoords="offset points", # xytextはこれに依存するらしい ha='center', va='bottom', ) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_autolabel.png') bar_autolabel()
ax.annotate
は矢印を作成するための関数だったが、矢印のやじりを消すことができたりテキストを入れることができるので、ここで流用されている。
ax.bar_label
で表示(楽ちん)
じゃあax.text
で独立して作成するしかないのかというとそういうわけでもない。実はax.bar_label
というコードも存在しており、ここにax.bar
の情報を入れることで自動的に値を表示してくれる。
しかも、ax.text
では位置を自分でわざわざ決めないといけなかったが、ax.bar_label
だと自動で中心にも上部にも配置することができる。場所が決まっているなら楽ちんだ。
# ax.textとサクッと値を表示(新しめ) # <https://matplotlib.org/stable/gallery/lines_bars_and_markers/barchart.html?highlight=grouped%20bar%20chart%20labels> def bar_barkabel(label_type='edge'): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') rects1 = ax.bar(x=x, height=height1, label='height1',) # 棒グラフに値を書き込む ax.bar_label(rects1, label_type=label_type) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig(f"bar_barkabel_{label_type}.png") # 棒グラフの上に値を書き込む bar_barkabel( label_type='edge' )
値を棒グラフの中心に持ってきたい場合はlabel_type='center'
とすればいい。超簡単。
2つ以上の棒グラフを並べる
棒グラフも折れ線グラフや散布図と同じように複数データを並べることがあるだろう。ここではそのように複数グラフを表示する方法を解説する。
シンプルに2本の棒グラフを並べる
ax.plot
のように普通に並べてax.bar
を書いてしまうとグラフ同士が重なってしまう。なので、複数本のグラフを描くときには位置をずらす必要がある。
# シンプルに2本並べる = 棒グラフが重なる def bar_2bar_simple(): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') ax.bar(x=x, height=height1, label='height1',) ax.bar(x=x, height=height2, label='height2', alpha=0.5) # 透明度を0.5に ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_2bar_simple.png') bar_2bar_simple()
位置を調節して棒グラフを並べる
それぞれの棒グラフの位置を調節する。両者の棒グラフの合計太さが太すぎると他の棒グラフと干渉するので、今回はwidth
で指定した棒の太さを最大値として、太さを分割することにする。
1つのx
に対する棒グラフ全体の太さをwidth
とすると、個々の棒グラフの太さはwidth / 2
(今は2データ)。個々の棒グラフの中心が棒グラフの位置する座標値なのでさらに/ 2
してあげる。この値をx
から引くと左側に、足すと右側に棒グラフが作成される。
# 2本の棒グラフを並べる def bar_2bar_width(width=0.8): bar_wid = width / 2 # それぞれの棒グラフの太さ bar_center = bar_wid / 2 # 棒グラフの開始位置のズレ幅 new_x = x - bar_center # ズレ幅を考慮したx new_x2 = x + bar_center # ズレ幅を考慮したx2 fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') ax.bar(x=new_x, height=height1, label='height1', width=bar_wid) ax.bar(x=new_x2, height=height2, label='height2', width=bar_wid, alpha=0.5) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_2bar_width.png') bar_2bar_width( width=0.8 # 2本棒グラフのトータル太さ )
棒グラフ群の太さは1
でもいいが1だとくっついてわかりにくいので0.8
とした。
複数本の棒グラフを自動調節して並べる
しかし、いつでもデータ数が2つとは限らない。3つかもしれないし4つかもしれない。はたまた1つかもしれない。状況が変わるたびにコードを変更するのは面倒なので自動的に調節するようにした。
よくわからんが上手いこといったのでそのまま示す。
def bar_nbar_width(width=0.8, **kwargs): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') for num, (name, hght) in enumerate(kwargs.items()): wid_each = width / len(kwargs) # 棒グラフ1本の太さ # よくわからんけど以下の処理で複数本棒グラフの位置決めがうまくいく odd_x = (2 * num + 1) / len(kwargs) # 奇数番目の棒グラフの右端の位置 shift_x = (odd_x - 1) / 2 # 棒グラフ群中心から測った各棒グラフの中心位置 adjust = width * shift_x # 棒グラフ群全体の幅で調節 new_x = x + adjust # 調節後のx bar = ax.bar(x=new_x, height=hght, label=name, width=wid_each) ax.bar_label(bar) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) # どのheightを使用したのかがわかるように文字列を作成 lst = [] for key in kwargs: pick = re.findall(r'\\d', key) # 数字部分だけ抽出 lst.append(*pick) # listで出力されるので展開して格納 name = ''.join(lst) # list内の要素を結合 fig.savefig(f"bar_nbar_width_{name}.png") # kwargsの本数を変更しても自動的に調節される kwargs = { 'height1': height1, 'height2': height2, 'height3': height3, 'height4': height4, } bar_nbar_width( **kwargs, width=0.8 # 2本棒グラフのトータル太さ )
グラフを保存する際にはre.findall
で数字部分のみを抽出し、文字列として結合してファイル名に組み込んだ。また、プロットする順番を入れ替えたりデータ数を変えたりしてもうまくいく。
色は特に設定していないのでズレているが、ちゃんとheight1
が各x
の値の中心に位置している。すごい。色は指定したかったら指定すればいい。
# kwargsの本数を変更しても自動的に調節される kwargs = { 'height4': height4, 'height1': height1, 'height2': height2, } bar_nbar_width( **kwargs, width=0.8 # 2本棒グラフのトータル太さ )
積み上げ式の棒グラフ
上で横に棒グラフを並べる方法について解説した。しかし、x
の数、height
の数が増えたりするとギチギチになってしまう。また、どれくらいの割合を占めるのかというようなこともわかりにくかったりするl。
そんな時は積み上げ式の棒グラフを使うと便利。上の画像では100%で描いているが、もちろん%表示じゃなくてもいい。
シンプル積み上げ
積み上げるには引数bottom
を設定すればいい。bottom
では描こうとしているプロットの下にどのプロットがあるのかを指定する。したがって、積み上げが増えると面倒になる。for
か何かで簡略化が必要。今は平文で書く。
また、ax.bar_label
でlabel_type='edge'
とすることでそのプロットまでの合計値を棒グラフの上に出してくれる。以下ではheight3
で'edge'
を設定しているので最終的な合計値が表示されている。
# simpleに積み上げ def bar_stack(): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') bottom12 = height1 # height2を描くときに下になるデータ bottom123 = height1 + height2 # height3を描くときに下になるデータ bar1 = ax.bar(x=x, height=height1, label='height1') bar2 = ax.bar(x=x, height=height2, label='height2', bottom=bottom12) bar3 = ax.bar(x=x, height=height3, label='height3', bottom=bottom123) ax.bar_label(bar1, label_type='center') # height1の値 ax.bar_label(bar2, label_type='center') # height2の値 ax.bar_label(bar3, label_type='center') # height3の値 ax.bar_label(bar3, label_type='edge') # 積み上げの合計値表示 ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_stack.png') bar_stack()
100%積み上げ
上での積み上げは値を足していった場合だったが、各値が全体の何%を占めているのかで表示することもあるだろう。しかし、plt
だけではこれはできないようなので、わざわざ計算することにする。仕方ない。
まずは一旦、height
達を2次元配列に格納する。
# height配列を1つの2次元配列に height_all = np.array([height1, height2, height3, height4]) print(height_all) # [[3. 1.5 5. 1.2 2.4] # [2.4 2.1 4.2 0.8 4.1] # [3.2 4.8 1. 0.2 4.4] # [1. 1.4 2.6 4.9 4.8]]
その後、列方向に合計し、各値をこの合計値で割って100をかけることで割合を計算することができる。
# 各xに対して合計 height_sum = np.sum(height_all, axis=0) print(height_sum) # [ 9.6 9.8 12.8 7.1 15.7] # heightの割合を計算 height_ratio = 100 * (height_all / height_sum) print(height_ratio) # [[31.25 15.30612245 39.0625 16.90140845 15.2866242 ] # [25. 21.42857143 32.8125 11.26760563 26.11464968] # [33.33333333 48.97959184 7.8125 2.81690141 28.02547771] # [10.41666667 14.28571429 20.3125 69.01408451 30.57324841]]
あとは作成した2次元配列を各データとして割り振る。
# データの割り振り height1_per, height2_per, height3_per, height4_per = height_ratio print(height1_per) # [31.25 15.30612245 39.0625 16.90140845 15.2866242 ] print(height2_per) # [25. 21.42857143 32.8125 11.26760563 26.11464968] print(height3_per) # [33.33333333 48.97959184 7.8125 2.81690141 28.02547771] print(height4_per) # [10.41666667 14.28571429 20.3125 69.01408451 30.57324841]
データができたら先ほど同様、積み上げ式の棒グラフを作成する。
# 100%積み上げ def bar_stack_100(): fig, ax = plt.subplots(1, 1) plt.subplots_adjust(right=0.85) ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y [%]') bottom12 = height1_per bottom123 = bottom12 + height2_per bottom1234 = bottom123 + height3_per bar1 = ax.bar(x=x, height=height1_per, label='height1') bar2 = ax.bar(x=x, height=height2_per, label='height2', bottom=bottom12) bar3 = ax.bar(x=x, height=height3_per, label='height3', bottom=bottom123) bar4 = ax.bar(x=x, height=height4_per, label='height4', bottom=bottom1234) ax.bar_label(bar1, label_type='center') ax.bar_label(bar2, label_type='center') ax.bar_label(bar3, label_type='center') ax.bar_label(bar4, label_type='center') ax.bar_label(bar4, label_type='edge') ax.legend(bbox_to_anchor=(1.0, 1.0)) ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('bar_stack_100.png') bar_stack_100()
横棒グラフ
いつだって縦に棒グラフを書くわけではない。横に描きたいこともあるだろう。基本的にはコードは同じだが、縦か横かの違いでコードが異なる。
シンプルな横棒グラフ
縦の棒グラフはax.bar
だったが横の場合はax.barh
となる。多分horizontalのhだろう。グラフが横向きになったので、グラフが横に伸びるとy
の値が大きいということになる。
ということで、今まで棒グラフの太さを示していたwidth
がheight
の代わりになる。また、xではなくy
が横軸のパラメータとなる。ややこしい。
# シンプルな横棒グラフ def barh_simple(): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('y') ax.set_ylabel('x') # 引数が変わる ax.barh(y=x, width=height1, label='height1',) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('barh_simple.png') barh_simple()
オプション適用済みの横棒グラフ
オプションの適用もの縦の時と同じように行うことができるが、height
がwidth
の代わりになることに注意。
# オプション適用済み def barh_option(color: tuple, edgecolor: str, linewidth: int or float, height: int or float): fig, ax = plt.subplots(1, 1) ax.set_title('title') ax.set_xlabel('y') ax.set_ylabel('x') # 引数が変わる ax.barh( y=x, width=height1, color=color, edgecolor=edgecolor, linewidth=linewidth, height=height, label='height1', ) ax.legend() ax.grid(which='major', ls='-') ax.grid(which='minor', ls='--', alpha=0.5) fig.savefig('barh_option.png') color = ( 'violet', 'darkviolet', 'palevioletred', 'mediumvioletred', 'blueviolet', ) barh_option( color=color, edgecolor='black', linewidth=2, height=1, )
線グラフとの組み合わせで真価が発揮?
今回はpltで棒グラフを作成する方法について解説した。今回は棒グラフしか使用していなかったが、使用用途としては気温と降水量など2種類のデータを使用する際に使われるだろう。
そんな中でさらによく使われるのが、棒グラフと線グラフの2軸グラフだ。この2軸グラフができるようになればもっと表現の手法が広がり、相手に伝わりやすくなるだろう。
関連記事
-
-
【plt&マーカー一覧】matplotlibのマーカーをグラフ化
続きを見る
-
-
【plt&subplot】pythonのpltで複数グラフを1つの画像に描く
続きを見る
-
-
【plt&mpld3】pltで作ったグラフを後から編集できるようにhtmlで保存
続きを見る
-
-
【plt&fill_between】matplotlibで領域を塗りつぶし
続きを見る