5
6

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.

matplotlib: 箱ひげ図 + 点のプロット

Last updated at Posted at 2019-02-10

(完成形ではなく、そのうち拡張します)

matplotlibで箱ひげ図を描くことがあったのですが、要望として「どのように各点が散らばっているか、図示してほしい」と言われました。
seabornを利用すれ簡単にできそうですが、matplotlibだけで試しました。

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pandas as pd

def show_boxplot(data, xvar, title="", ylabel=""):
    '''
    data:   taple data
    xvar:   array label data for each value name
    title:  string
    ylabel: string
    '''
    fig, ax = plt.subplots()
    bp = ax.boxplot(data)
    ax.set_xticklabels(xvar)
    ax.set_ylabel(ylabel)
    ax.set_title(title)

    for y, i in zip(data, range(len(xvar))):
        # "jitter" to the x-axis 
        x = np.random.normal(i+1, 0.05, size=len(y))
        ax.plot(x, y, '.', color="C{}".format(i))

    ax.grid()
    plt.show()

data1 = np.random.normal(50, 10, 100)
data1 = np.append(data1, 100)             # 外れ値
data2 = np.random.normal(45, 15, 100)
xvar = ["data1", "data2"]
show_boxplot((data1, data2), xvar)

以下のように表示されます。
matplot_boxplot.png

5
6
0

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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?