LoginSignup
8

More than 3 years have passed since last update.

matplotlibのboxplotの色を変える

Last updated at Posted at 2019-03-19

複数のboxを同じ色で塗る

plot_out_1.jpg

#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt

# test data
r1=(np.random.randn(100) * 10) + 10
r2=(np.random.randn(100) * 10) + 50
data=[r1, r2]


# boxplot
fig=plt.figure(figsize=(5,2.5))
ax=fig.add_subplot(111)
ax.boxplot(data,
           vert=False,  # 横向きにする
           patch_artist=True,  # 細かい設定をできるようにする
           widths=0.5,  # boxの幅の設定
           boxprops=dict(facecolor='#1E90FF80',  # boxの塗りつぶし色の設定
                         color='black', linewidth=1),  # boxの枠線の設定
           medianprops=dict(color='black', linewidth=1),  # 中央値の線の設定
           whiskerprops=dict(color='black', linewidth=1),  # ヒゲの線の設定
           capprops=dict(color='black', linewidth=1),  # ヒゲの先端の線の設定
           flierprops=dict(markeredgecolor='black', markeredgewidth=1)  # 外れ値の設定
           )

plt.savefig('plot_out.pdf')

複数のboxを異なる色で塗る

plot_out_2.jpg

#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt

# test data
r1=(np.random.randn(100) * 10) + 10
r2=(np.random.randn(100) * 10) + 50
data=[r1, r2]


# boxplot
fig=plt.figure(figsize=(5,2.5))
ax=fig.add_subplot(111)
bp=ax.boxplot(data,
              vert=False,  # 横向きにする
              patch_artist=True,  # 細かい設定をできるようにする
              widths=0.5,  # boxの幅の設定
              medianprops=dict(color='black', linewidth=1),  # 中央値の線の設定
              whiskerprops=dict(color='black', linewidth=1),  # ヒゲの線の設定
              capprops=dict(color='black', linewidth=1),  # ヒゲの先端の線の設定
              flierprops=dict(markeredgecolor='black', markeredgewidth=1)  # 外れ値の設定
              )

# boxの色のリスト
# '#1E90FF'=dodgerblue, '#8A2BE2'=blueviolet, 最後に80をつけると、alpha=0.5
colors=['#1E90FF80', '#8A2BE280']

# boxの色の設定
for b, c in zip(bp['boxes'], colors):
    b.set(color='black', linewidth=1)  # boxの外枠の色
    b.set_facecolor(c) # boxの色

plt.savefig('plot_out.pdf')

線の色も変える

plot_out_3.jpg

#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt

# test data
r1=(np.random.randn(100) * 10) + 10
r2=(np.random.randn(100) * 10) + 50
data=[r1, r2]


# boxplot
fig=plt.figure(figsize=(5,2.5))
ax=fig.add_subplot(111)
bp=ax.boxplot(data,
              vert=False,  # 横向きにする
              patch_artist=True,  # 細かい設定をできるようにする
              widths=0.5  # boxの幅の設定
              )

# 色のリスト
# '#1E90FF'=dodgerblue, '#8A2BE2'=blueviolet, 最後に80をつけると、alpha=0.5
colors=['#1E90FF80', '#8A2BE280'] # boxes, medians, fliers
colors2=['#1E90FF80', '#1E90FF80', '#8A2BE280', '#8A2BE280'] # whiskers, caps

# boxの色の設定
for b, c in zip(bp['boxes'], colors):
    b.set(color=c, linewidth=1)  # boxの外枠の色
    b.set_facecolor(c) # boxの色
# 中央値の線の設定
for b, c in zip(bp['medians'], colors):
    b.set(color=c, linewidth=1)
# ヒゲの線の設定
for b, c in zip(bp['whiskers'], colors2):
    b.set(color=c, linewidth=1)
# ヒゲの先端の線の設定
for b, c in zip(bp['caps'], colors2):
    b.set(color=c, linewidth=1)
# 外れ値の設定
for b, c in zip(bp['fliers'], colors):
    b.set(markeredgecolor=c, markeredgewidth=1)

plt.savefig('plot_out.pdf')

seabornに任せてしまう

plot_out_4.jpg

#!/usr/bin/env python3

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

# test data
r1=(np.random.randn(100) * 10) + 10
r2=(np.random.randn(100) * 10) + 50
data=[r1, r2]

# boxplot
fig=plt.figure(figsize=(5,2.5))
ax=fig.add_subplot(111)
sns.boxplot(data=data,
            orient='h',  # 横向きにする
            palette='Pastel1',  # 色
            width=0.5  # boxの幅の設定
            )

plt.savefig('plot_out.pdf')

環境

Ubuntu 18.04
Python 3.7.2
matplotlib 3.0.3
seaborn 0.9.0
numpy 1.15.4

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
8