LoginSignup
9
6

More than 5 years have passed since last update.

【Python】matplotlibでヒストグラムを描画する際のエラーの対処方法

Last updated at Posted at 2016-07-07

matplotlibでヒストグラムを描画する際に出たエラーの対処方法。

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

df = pd.DataFrame(np.random.randn(4, 4))
df.ix[0, 0] = np.nan
df
出力結果
          0         1         2         3
0       NaN -1.778988 -0.154485  0.991733
1  0.813296 -0.493322 -0.667192  0.555212
2 -0.207708 -1.197387 -1.101739 -0.796314
3 -0.037038 -1.903817  0.342002 -0.648974

欠損値がある列のヒストグラムを描画しようとするとエラーが出る。

plt.hist(df[0], alpha=0.5, bins=10)
出力結果
ValueError: max must be larger than min in range parameter.

この場合,欠損値を除けばエラーが出なくなる。

plt.hist(df[0].dropna(), alpha=0.5, bins=10)
出力結果
(array([ 1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]),
 array([-0.2077081 , -0.10560772, -0.00350734,  0.09859304,  0.20069342,
         0.3027938 ,  0.40489418,  0.50699456,  0.60909493,  0.71119531,
         0.81329569]),
 <a list of 10 Patch objects>)
9
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
9
6