AnyPointSystem (powered by Qiitadon) > 2019-05-18 UI > Bullet Journal (Habitat Tracker) > Japanize_matplotlibの使用停止 + 見た目の改善(pcolor()使用) | Japanize_matplotlibのひっかかり

```txt:動作環境
Ubuntu 18.04 LTs
Jupyter Notebook v5.7.8 
(Python 3.6.7 (default, Oct 22 2018, 11:32:17) )
```

### 関連

- [Qiitadon (Mastodon) > create_app()のto_fileで保存されるclient_id.txtの中身 | AnyPointSystem始めました](https://qiita.com/7of9/items/f8deafc9c301d9e01e2b)
- [AnyPointSystem (powered by Qiitadon) > 2019-04-27 システム設計](https://qiita.com/7of9/items/45aba268f6358541080c)

### 前準備
(2019-05-08改訂)

1. Mastodon.pyのセットアップ
2. ~~japanize_matplotlibのセットアップ~~
    - 今回の変更で不要になる
3. アプリの登録 (Qiitadonの開発にて登録「AnyPointSystem」）
4. client_id.txtの生成 (client_idとclient_secret: 手順3の登録情報から)
5. access_token.txtの生成 (手順3の登録情報から)

### 概要

- Qiitadonをデータベースとするポイントシステム
- 今回
    - Japanize_matplotlibの使用停止
        - ライブラリの不具合の回避
        - 日本語文字化けの対処
            - 変更前: Japanize_matplotlib
            - 変更後: stdout
    - 見た目の改善
        - カレンダ文字列の表示位置を５項目単位に
        - 色表示の枠線追加
            - imshow()からpcolor()へ変更

### 関連

- [matplotlib.axes.Axes.pcolor](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.pcolor.html#matplotlib.axes.Axes.pcolor)

### 実装

```py:HabitatTracker_190518.ipynb
import sys
from mastodon import Mastodon
import datetime
import csv
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm

# import japanize_matplotlib
#     Reason: Japanize_matplotlib causes complex error in Ubuntu 16.04 LTS

# [ ] PEP8 checked
#    except for "line too long (81 > 79 characters)"

# 2019-05-18 09:35 refactor
# 2019-05-18 08:52 branched from HabitatTracker_190512
#     - change from imshow() to pcolor()
#     reason: continuous days with same values could not be separately seen
# 2019-05-18 08:38 tweak stdout and Figures stack
# 2019-05-16 00:02 stack stdout and Figures
# 2019-05-12 10:22 remove japanize_matplotlib
#     Reason: Japanize_matplotlib causes error in Ubuntu 16.04 LTS
#     - add getCharString()
# 2019-05-12 10:03
#     - fix bug > list items are less than expected
# 2019-05-06 08:57
#     - correct date from [UTC] to [JST]
#     - fix bug: calender days had off-by-one-error
# 2019-05-05 10:44
# 2019-05-05 09:18
# ...
# 2019-04-29 08:49
# 2019-04-27 09:17

# reference
# for bullet journal display
#   https://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html

url = "https://qiitadon.com"
cid_file = 'client_id.txt'
token_file = 'access_token.txt'

mastodon = Mastodon(
    client_id=cid_file,
    access_token=token_file,
    api_base_url=url
)

# print(user_toots[0])  # トゥートを全て表示する

NUM_LOOP = 80  # for getting 40 toot each
# NUM_LOOP = 0  # for debug
# NUM_LOOP = 5 # for debug


def isAnyPointData(inlist):
    for elem in inlist:
        # print('***', elem)  # debug
        if 'anypointdata' in elem['name']:
            return True
    return False


def getPointTag(inlist):
    res = []
    for elem in inlist:
        if 'anypointdata' in elem['name']:
            continue
        res += [elem['name']]
    return res


max_id = None
fulllist = []

cnt = 0
for loop in range(NUM_LOOP):
    user_dict = mastodon.account_verify_credentials()
    # 40 seems limit as of 2019-04-27
    user_toots = mastodon.account_statuses(user_dict['id'],
                                           max_id=max_id, limit=40)

    max_id = user_toots[-1]['id']
    # print(len(user_toots))
    for idx in range(len(user_toots)):
        # print(user_toots[idx])  # to print what the toot looks like
        atoot = user_toots[idx]['tags']
        atime = user_toots[idx]['created_at']

        # to avoid [TypeError: can't subtract offset-naive and offset-aware datetimes ]
        # https://stackoverflow.com/questions/796008/cant-subtract-offset-naive-and-offset-aware-datetimes
        atime = atime.replace(tzinfo=None)

        if len(atoot) == 0:
            continue
        if not isAnyPointData(atoot):
            continue
        res = getPointTag(atoot)
        fulllist += [[atime, res]]
        print('.', end='')
        sys.stdout.flush()
        cnt = cnt + 1
        if cnt % 60 == 59:
            print()  # for CRLF
            sys.stdout.flush()
        # print(res)
        # print(atime)

# for debug
if NUM_LOOP == 0:
    fulllist = [
        [datetime.datetime(2019, 4, 30, 23, 8, 29, 983000), ['中国語学習']],
        [datetime.datetime(2019, 4, 30, 23, 1, 52, 859000), ['英語復習']],
        [datetime.datetime(2019, 4, 30, 13, 25, 46, 588000), ['中国語復習']],
        [datetime.datetime(2019, 4, 30, 13, 1, 31, 340000), ['ツール作成']],
        [datetime.datetime(2019, 4, 30, 12, 58, 27, 258000), ['animalbyaurora']],
        [datetime.datetime(2019, 4, 30, 9, 40, 31, 145000), ['チカッとチカ千花っ']],
        [datetime.datetime(2019, 4, 30, 9, 21, 52, 735000), ['farbymarieueda']],
        [datetime.datetime(2019, 4, 30, 9, 14, 35, 939000), ['queendombyaudora']],
        [datetime.datetime(2019, 4, 30, 9, 10, 58, 67000), ['animalbyaurora']],
        [datetime.datetime(2019, 4, 30, 2, 59, 50, 350000), ['英語復習']],
        [datetime.datetime(2019, 4, 29, 23, 32, 33, 842000), ['grenning学習', 'tdd学習']],
        [datetime.datetime(2019, 4, 29, 23, 27, 7, 216000), ['英語復習']],
        [datetime.datetime(2019, 4, 29, 13, 21, 5, 351000), ['英語学習']],
        [datetime.datetime(2019, 4, 29, 13, 5, 28, 552000), ['chinese']],
        [datetime.datetime(2019, 4, 29, 12, 30, 45, 331000), ['中国語学習']],
        [datetime.datetime(2019, 4, 29, 11, 57, 15, 318000), ['farbymarieueda']],
        [datetime.datetime(2019, 4, 29, 9, 51, 2, 24000), ['チカッとチカ千花っ']],
        [datetime.datetime(2019, 4, 29, 9, 33, 31, 33000), ['queendombyaudora']],
        [datetime.datetime(2019, 4, 29, 9, 32, 7, 586000), ['英語学習']],
        [datetime.datetime(2019, 4, 29, 9, 26, 30, 169000), ['farbymarieueda']],
        [datetime.datetime(2019, 4, 29, 9, 22, 31, 857000), ['animalbyaurora']],
        [datetime.datetime(2019, 4, 29, 1, 30, 51, 103000), ['ツール作成']],
        [datetime.datetime(2019, 4, 29, 1, 29, 41, 267000), ['チカッとチカ千花っ']],
        [datetime.datetime(2019, 4, 28, 23, 25, 35, 632000), ['queendombyaudora']],
        [datetime.datetime(2019, 4, 28, 23, 18, 31, 362000), ['animalbyaurora']],
        [datetime.datetime(2019, 4, 28, 23, 0, 21, 950000), ['英語復習']],
        [datetime.datetime(2019, 4, 28, 12, 36, 59, 80000), ['中国語復習']],
        [datetime.datetime(2019, 4, 28, 3, 34, 23, 115000), ['queendombyaurora']],
        [datetime.datetime(2019, 4, 28, 3, 30, 38, 750000), ['animalbyaurora']],
        [datetime.datetime(2019, 4, 28, 3, 29, 39, 44000), ['中国語学習']],
        [datetime.datetime(2019, 4, 28, 1, 18, 9, 823000), ['grenning学習', 'tdd学習']],
        [datetime.datetime(2019, 4, 28, 1, 17, 45, 883000), ['チョコ食べる']],
    ]

print()  # for new line

# add 1st tag
tagcsv = fulllist[0][1][0]  # 0: 1st toot, 1: tag, 0: expand from list
# print(tagcsv)
for elem in fulllist:
    # print(elem[1])
    atag = elem[1][0]
    if atag not in tagcsv:
        tagcsv += "," + str(elem[1][0])
taglist = tagcsv.split(",")

DAYS_PAST = 40
BASE_DATE = datetime.datetime.now() - datetime.timedelta(days=(DAYS_PAST-1))
BASE_DATE = BASE_DATE.replace(hour=0, minute=0, second=0, microsecond=0)
# print(BASE_DATE)
NUM_GRAPHS = 30
FIG_SIZE = figsize = (15.9, 0.3)
NUM_COLORS = 5


def getCharString(idx):
    return "(%c)" % (ord('a') + idx)


def drawImage(ax, wrklist):
    # print(wrklist)
    ary = np.array(wrklist)
    ary = np.reshape(ary, (1, -1))  # bar display
    # print(ary)
    # ax.imshow(ary, cmap='Paired', vmin=0, vmax=NUM_COLORS)
    ax.pcolor(ary, cmap='Paired', edgecolor='k', vmin=0, vmax=NUM_COLORS)
    # ax.set_title(atag, loc='left')


def makeCalenderDaysString(startdt, loopcount):
    res = ''
    #for idx in range(2, loopcount + 1):  # 2, 1: adjusted so that today is the right most part
    for idx in range(1, loopcount):  # 1: adjusted so that today is the right most part
        wrkdt = startdt + datetime.timedelta(idx)
        wrkstr = str(wrkdt.day)
        if len(wrkstr) < 2:
            res += '|  ' + wrkstr
        else:
            res += '|' + wrkstr
    return res


def printCalenderDaysString():
    wrklist = [(i % 2) for i in range(DAYS_PAST)]  # to make striped pattern
    trgtag = makeCalenderDaysString(BASE_DATE, DAYS_PAST)
    print(' ', trgtag)  # ' ' to adjust horizontal position with matplotlib
    sys.stdout.flush()

# add each item
amax = 1
for idx, trgtag in enumerate(taglist):  # loop by target tag
    wrktag = getCharString(idx)
    print(wrktag, trgtag)
    sys.stdout.flush()
    fig, ax = plt.subplots(1, 1, figsize=FIG_SIZE,
                           subplot_kw={'xticks': [], 'yticks': []})
    fig.subplots_adjust(hspace=0, wspace=0.3)

    wrklist = [0 for i in range(DAYS_PAST)]
    # print(trgtag)
    for elem in fulllist:
        adate = elem[0]
        tags = elem[1]
        for atag in tags:
            if trgtag not in atag:
                continue
            elem[0] = elem[0] + datetime.timedelta(hours=9)  # [UTC] to [JST]
            # print(elem[0])  # debug
            elpsd = (int)((elem[0] - BASE_DATE).total_seconds() / 60 / 60 / 24)
            wrklist[elpsd] += 1
            # print(elpsd, adate, atag)

    if idx % 5 == 0:
        printCalenderDaysString()

    # print(wrklist)
    if amax < max(wrklist):
        amax = max(wrklist)
    if idx < len(taglist) + 1:  # 1: 1st is the calendar days
        # May 12, 2019 to avoid Japanize_matplotlib error
        wrktag = getCharString(idx) + trgtag
        drawImage(ax, wrklist)
    else:
        wrklist = range(NUM_COLORS)
        drawImage(ax, wrklist)
    plt.show()


# draw colormap
idx = len(taglist) + 1
wrklist = range(NUM_COLORS)
print('colormap')
FIG_SIZE = figsize = (2, 0.3)
fig, ax = plt.subplots(1, 1, figsize=FIG_SIZE,
                       subplot_kw={'xticks': [], 'yticks': []})
drawImage(ax, wrklist)
plt.show()

```

### 結果


![Screenshot from 2019-05-18 09-41-34.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/32870/73c08c92-19c1-1c8a-3ed7-d5d8460bd8c7.png)


## Japanize_matplotlibのひっかかり

Japanize_matplotlib
日本語の文字化けを解消する良いライブラリ。

自分の環境では

- Ubuntu 18.04 LTS: 正常動作
- Ubuntu 16.04 LTS: エラー

エラーの解消について調査をしたが、根が深そうで明確な対処にはさらなる調査が必要に思えた。

自分一人が対処するのであればそれでも構わない。多数の人の使用を考慮すると、根が深いエラーがあるライブラリの使用は躊躇してしまう（ライブラリは良いものであるのに）。

今回は使用を停止した。
stdoutを使うことで対処。

**一方で、[Radar Chart](https://qiita.com/7of9/items/02fa01cd5300512b2438)のようにJapanize_matplotlibなしで対処が難しい図もある。**

### imshow()からpcolor()変更時の知見

imshow()とpcolor()では同じfigsizeとしても、高さ方向の定義が違うようだ。
pcolor()の方が大きい。

以下のような設定にしたところ、同じような高さにできた。

- imshow()
    - `FIG_SIZE = figsize = (15.9, 20)`
- pclor()
    - `FIG_SIZE = figsize = (15.9, 0.3)`