Last active
March 23, 2021 14:01
-
-
Save ultimatile/b8d9551de955580e53f04502f1e35084 to your computer and use it in GitHub Desktop.
template script for line plotting with matplotlib.pyplot
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
#Fermi分布函数をmatplotlibで書いてみる | |
# 一般的な注意 | |
# matplotlibはmatlab流とobject流で呼び出す函数名が変わるので注意が必要. ここで使っているのはobject流. | |
# 細かい調整はobject流の方ができるのでobject流をお勧めする. | |
# 参考文献は例えば https://qiita.com/skotaro/items/08dc0b8c5704c94eafb9 | |
import matplotlib.pyplot as plt | |
import numpy as np | |
# pdfで保存するのに必要 | |
from matplotlib.backends.backend_pdf import PdfPages | |
# Fermi分布函数(mu=0) | |
fermi = lambda E, T: 1 / (1 + np.exp(E / T)) | |
# 使用するcolormapを指定(colormapは下のurlで確認可能) | |
# https://matplotlib.org/tutorials/colors/colormaps.html | |
cmap = plt.get_cmap("brg") | |
# plotする線種を指定, 順に実線, 破線, 一点破線, 点線. | |
# 線種は以下のurlで確認可能, 破線の間隔などさらに細かく指定することも可能 | |
# https://matplotlib.org/gallery/lines_bars_and_markers/line_styles_reference.html | |
lslist = ['-', '--', '-.', ':'] | |
# graphの設定をいじる方法は描画する函数に渡すオプションなどで指定する方法とrcParamsとpyplot共通のparameter値をいじる方法がある | |
# 後者は複数のgraphの設定を一括で変更できる. default値をいじってると言ってもいい | |
# rcParamsの設定をいじる | |
plt.rcParams['font.family'] ='sans-serif' # 使用するフォント | |
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 # 軸の線幅 | |
# xy軸のlabelのfontsizeをrcParamsで指定 | |
# ticksize=24 | |
# plt.rcParams['xtick.labelsize'] = ticksize | |
# plt.rcParams['ytick.labelsize'] = ticksize | |
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 = 2 | |
# 保存するfile名を設定 | |
fname = "filename" | |
samples = 100 | |
Emin = -2.5 | |
Emax = 2.5 | |
E = np.linspace(Emin, Emax, samples) | |
# 凡例がlabelの太さlw, 線種lsでcolor色の透明度alphaの描画 | |
# T=0, 階段函数 | |
ax.plot(E, np.heaviside(-E, 1), alpha=0.65, label=r"$T=0$", color=cmap(0), ls=lslist[0], lw=lw) | |
T = 0.2 | |
ax.plot(E, fermi(E, T), alpha=0.65, label=r"$T=$" + "{}".format(T), color=cmap(0.2), ls=lslist[1], lw=lw) | |
T = 0.4 | |
ax.plot(E, fermi(E, T), alpha=0.65, label=r"$T=$" + "{}".format(T), color=cmap(0.4), ls=lslist[2], lw=lw) | |
T = 0.6 | |
ax.plot(E, fermi(E, T), alpha=0.65, label=r"$T=$" + "{}".format(T), color=cmap(0.6), ls=lslist[3], lw=lw) | |
# graphで描画する範囲を指定 | |
ax.set_xlim(Emin, Emax) | |
ax.set_ylim(0, 1.05) | |
# タイトル | |
ax.set_title(r"Fermi distribution", size=24) | |
# 細かく指定してグリッド表示 | |
ax.grid(color='k', linestyle=':', linewidth=0.5, alpha=0.5) | |
# とりあえずグリッド表示 | |
# ax.grid(True) | |
# graph中に文字を入れる | |
# xy軸の値で指定 | |
# ax.text(0.2, 0.2, r"$f=\exp(-\frac{E}{T})$" ,size=20) | |
# 箱の中の相対座標で指定(左下を基準にして値は0~1) | |
ax.text(0.1, 0.1, r"$f=\frac{1}{1+\exp(\frac{E}{T})}$", size=20, transform=ax.transAxes) | |
# 余白の設定 | |
# 各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) | |
# labelがはみ出るときなどに使うとはみ出なくなることがある | |
# plt.tight_layout() | |
# 凡例の表示 | |
# locで置き場所を指定できる | |
# defaultの場所は自動的に決まる | |
ax.legend(fontsize=20) #,loc='lower right') | |
# xy軸のlabelを設定 | |
ax.set_xlabel(r'$E$') #, size=20) | |
ax.set_ylabel(r'$f$') #, size=20) | |
# interactive modeで表示 | |
plt.show() | |
# pdfで保存 | |
# pp = PdfPages('{}.pdf'.format(fname)) | |
# pp.savefig(fig) | |
# pp.close() | |
# epsで保存 | |
# plt.savefig('{}.eps'.format(fname), metadata="eps") | |
# pngで保存 | |
# plt.savefig('{}.png'.format(fname)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment