LoginSignup
6
9

More than 5 years have passed since last update.

matplotlibで特定のデータをプロットしない方法

Last updated at Posted at 2019-02-26

はじめに

漫画の掲載順を取得してグラフ化する記事を書いている時に休載で掲載順を取得できない月があり、グラフ化するときに困った。
matplotlibで特定のデータをプロットしない方法が後から分かったのでメモ。

環境

# uname -a                                                                                                                              
Linux kali 4.18.0-kali2-amd64 #1 SMP Debian 4.18.10-2kali1 (2018-10-09) x86_64 GNU/Linux
# python --version                                                                                                                             
Python 3.7.2+
# pip3 show matplotlib                                                                                                                         
Name: matplotlib
Version: 2.2.2

問題

例えば、以下のようなデータがあったとする。

month 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月
data 13 15 21 5 10   21 17 15 16 21 13

6月だけデータがない。

Y_data=[13,15,21,5,10, ,21,17,15,16,21,13]

Y軸のデータの配列に空きができてしまった。どうすればいいのか。

解決法

プロットしたくないor欠損しているデータにはNoneを代入するとその項目はプロットされなくなる。

Y_data=[13,15,21,5,10,None,21,17,15,16,21,13]

こんな感じ。

#!/usr/bin/env python 
import matplotlib.pyplot as plt

X_data=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Y_data=[13,15,21,5,10,None,21,17,15,16,21,13]
month_name=['Jan.','Feb.','Mar.','Apr.','May','Jun.','Jul.','Aug.','Sep.','Oct.','Nov.','Dec.']

plt.xlabel('month')
plt.ylabel('data')

plt.xticks(X_data,month_name)
plt.plot(X_data,Y_data)
plt.show()

結果
plot_None.png

一件落着。

参考文献

python - matplotlibにおけるグラフの始点の指定 - スタック・オーバーフロー
これのお陰で助かりました。ありがとうございます。

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