Last active
September 8, 2021 17:14
-
-
Save ultimatile/0fb60a2804b943baa9049ed675c4da94 to your computer and use it in GitHub Desktop.
日付-ページ数プロット. 論文の進捗管理などにどうぞ. 進捗を書き込んだprogress_.dat読み込んで表示します.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import numpy as np | |
import datetime | |
from matplotlib.dates import DateFormatter | |
# pdfで保存するのに必要 | |
from matplotlib.backends.backend_pdf import PdfPages | |
# 使用するcolormapを指定(colormapは下のurlで確認可能) | |
# https://matplotlib.org/tutorials/colors/colormaps.html | |
cmap = plt.get_cmap("brg") | |
# rcParamsの設定をいじる | |
# 日本語用, IPAexGothicを使うには基本的にinstallが必要(https://moji.or.jp/ipafont/ipafontdownload/ ←ここからdownload) | |
# あるいはすでにinstallされている日本語fontを指定すればよい | |
# fontのinstallなどmatplotlibで日本語を使うことに関する情報は例えば↓ | |
# https://qiita.com/mikan3rd/items/791e3cd7f75e010c8f9f | |
# plt.rcParams['font.family'] = ['IPAexGothic'] | |
plt.rcParams['font.size'] = 24 # まとめてfontsizeを設定 | |
plt.rcParams['xtick.direction'] = 'in' # x軸の目盛線が内向き('in')か外向き('out')か双方向か('inout') | |
plt.rcParams['ytick.direction'] = 'in' # y軸の目盛線が内向き('in')か外向き('out')か双方向か('inout') | |
plt.rcParams['xtick.major.width'] = 1.0 # x軸主目盛り線の線幅 | |
plt.rcParams['ytick.major.width'] = 1.0 # y軸主目盛り線の線幅 | |
plt.rcParams['axes.linewidth'] = 1 # 軸の線幅 | |
if __name__ == "__main__": | |
# graph全体の実体作成 | |
# figsizeはgraph全体の大きさの指定 | |
# width, heightの順, 単位はインチ | |
fig = plt.figure(figsize=(8, 6)) | |
#graphの実体作成 | |
# 111の意味は縦方向に1つ横方向に1つ箱を作ってその1番目という意味の省略記法 | |
ax = fig.add_subplot(111) | |
# xy軸のlabelのfontsizeを直接指定 | |
labelsize = 32 | |
ax.xaxis.label.set_size(labelsize) | |
ax.yaxis.label.set_size(labelsize) | |
ax.tick_params(labelsize=24)# xy軸の文字サイズ | |
ax.tick_params(axis='x', pad=12)# x軸の文字をpadだけ下げる(原点のxy軸の文字が被るのを防ぐため) | |
# 描画するgraphの線の太さを指定するための変数 linewidth | |
lw = 1.5 | |
# 保存するfile名を設定 | |
fname = "progress" | |
# 日付labelの設定 | |
data = np.genfromtxt("progress_.dat",dtype=int)# dataはspace区切りでyear, month, day, total pages | |
xdata = [ datetime.date(datum[0], datum[1], datum[2]) for datum in data] | |
ydata = data[:, 3] | |
ax.plot_date(xdata, ydata, color=cmap(0), ls='-', lw=lw, marker='x', alpha=0.8, ms=8, mew=2) | |
# graphで描画する範囲を指定 | |
dstart = datetime.date(2021, 7, 31)# 開始日 | |
dend = datetime.date(2022, 2, 1)# 終了日 | |
ax.set_xlim(dstart, dend) | |
ax.set_ylim(0, np.int(1.5 * np.max(ydata))) | |
# タイトル | |
ax.set_title(r"Thesis Progress", size=24) | |
# ax.set_title(r"博士論文進捗", size=24)# 日本語を表示するには17行目で日本語fontを指定する必要がある | |
# 細かく指定してグリッド表示 | |
ax.grid(color='k', linestyle=':', linewidth=0.5, alpha=0.5) | |
# 余白の設定 | |
# 各optionの意味は公式や以下参照 | |
# 参考 https://qiita.com/ultimatile/items/bc76104a17e05b8d9388 | |
plt.subplots_adjust(top=0.92, bottom=0.2, left=0.15, right=0.95, hspace=0, wspace=0) | |
formatter = DateFormatter('%Y/%m/%d')# 日付の表示を年/月/日に指定 | |
ax.xaxis.set_major_formatter(formatter) | |
ax.xaxis.set_tick_params(rotation=30, labelsize=15) | |
# xy軸のlabelを設定 | |
ax.set_ylabel(r'Page') #, size=20) | |
# ax.set_ylabel(r'page数') #, size=20)# 日本語を表示するには17行目で日本語fontを指定する必要がある | |
# interactive modeで表示 | |
plt.show() | |
# pdfで保存 | |
pp = PdfPages('{}.pdf'.format(fname)) | |
pp.savefig(fig) | |
pp.close() | |
# pngで保存 | |
plt.savefig('{}.png'.format(fname)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 年 月 日 ページ数 | |
2021 8 1 1 | |
2021 8 2 2 | |
2021 8 3 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment