Skip to content

Instantly share code, notes, and snippets.

@ultimatile
Created April 10, 2021 21:09
Show Gist options
  • Save ultimatile/d482df88a62304324c671278d1622092 to your computer and use it in GitHub Desktop.
Save ultimatile/d482df88a62304324c671278d1622092 to your computer and use it in GitHub Desktop.
julia version of template_plt.py https://gist.github.com/ultimatile/b8d9551de955580e53f04502f1e35084 using PyPlot.jl
using PyPlot
# https://github.com/JuliaPy/PyPlot.jl
#Fermi分布函数をmatplotlibで書いてみる
# 一般的な注意
# matplotlibはmatlab流とobject流で呼び出す函数名が変わるので注意が必要. ここで使っているのはobject流.
# 細かい調整はobject流の方ができるのでobject流をお勧めする.
# 参考文献は例えば https://qiita.com/skotaro/items/08dc0b8c5704c94eafb9
# Fermi分布函数(mu=0)
fermi(E, T) = 1 / (1 + exp(E / T))
# heaviside函数
heaviside(x1; x2=0) = ifelse(x1 < 0, zero(x1), ifelse(x1 > 0, one(x1), oftype(x1, x2)))
# 使用するcolormapを指定(colormapは下のurlで確認可能)
# https://matplotlib.org/tutorials/colors/colormaps.html
cmap = ColorMap("brg")
# plotする線種を指定, 順に実線, 破線, 一点破線, 点線.
# 線種は以下のurlで確認可能, 破線の間隔などさらに細かく指定することも可能
# https://matplotlib.org/gallery/lines_bars_and_markers/line_styles_reference.html
lslist = ["-", "--", "-.", ":"]
# graphの設定をいじる方法は描画する函数に渡すオプションなどで指定する方法とrcParamsとpyplot共通のparameter値をいじる方法がある
# 後者は複数のgraphの設定を一括で変更できる. default値をいじってると言ってもいい
# rcParamsの設定をいじる
rcParams = PyPlot.PyDict(PyPlot.matplotlib."rcParams")
rcParams["font.family"] ="sans-serif" # 使用するフォント
rcParams["font.size"] = 24 # まとめてfontsizeを設定
rcParams["xtick.direction"] = "in" # x軸の目盛線が内向き('in')か外向き('out')か双方向か('inout')
rcParams["ytick.direction"] = "in" # y軸の目盛線が内向き('in')か外向き('out')か双方向か('inout')
rcParams["xtick.major.width"] = 1.0 # x軸主目盛り線の線幅
rcParams["ytick.major.width"] = 1.0 # y軸主目盛り線の線幅
rcParams["axes.linewidth"] = 1 # 軸の線幅
# xy軸のlabelのfontsizeをrcParamsで指定
# ticksize = 24
# rcParams['xtick.labelsize'] = ticksize
# rcParams['ytick.labelsize'] = ticksize
# graph全体の実体作成
# figsizeはgraph全体の大きさの指定
# width, heightの順, 単位はインチ
fig = figure(figsize=(8, 6))
#graphの実体作成
# 111の意味は縦方向に1つ横方向に1つ箱を作ってその1番目という意味の省略記法
ax = 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 = 10000
Emin = -2.5
Emax = 2.5
E = range(Emin, Emax, length=samples)
# 凡例がlabelの太さlw, 線種lsでcolor色の透明度alphaの描画
# T=0, 階段函数
ax.plot(E, heaviside.(-E), alpha=0.65, label=L"T=0", color=cmap(0), ls=lslist[1], lw=lw)
T = 0.2
ax.plot(E, fermi.(E, T), alpha=0.65, label=L"T="*"$T", color=cmap(0.2), ls=lslist[2], lw=lw)
T = 0.4
ax.plot(E, fermi.(E, T), alpha=0.65, label=L"T="*"$T", color=cmap(0.4), ls=lslist[3], lw=lw)
T = 0.6
ax.plot(E, fermi.(E, T), alpha=0.65, label=L"T="*"$T", color=cmap(0.6), ls=lslist[4], lw=lw)
# graphで描画する範囲を指定
ax.set_xlim(Emin, Emax)
ax.set_ylim(0, 1.05)
# タイトル
ax.set_title("Fermi distribution", size=24)
# 細かく指定してグリッド表示
ax.grid("on", color="k", linestyle=":", linewidth=0.5, alpha=0.5)
# とりあえずグリッド表示
# ax.grid(true, color="k")
# graph中に文字を入れる
# xy軸の値で指定
# ax.text(0.2, 0.2, L"f=\exp(-\frac{E}{T})" ,size=20)
# 箱の中の相対座標で指定(左下を基準にして値は0~1)
ax.text(0.1, 0.1, L"f=\frac{1}{1+\exp(\frac{E}{T})}", size=20, transform=ax.transAxes)
# 余白の設定
# 各optionの意味は公式や以下参照
# 参考 https://qiita.com/ultimatile/items/bc76104a17e05b8d9388
subplots_adjust(top=0.92, bottom=0.2, left=0.15, right=0.95, hspace=0, wspace=0)
# labelがはみ出るときなどに使うとはみ出なくなることがある
# tight_layout()
# 凡例の表示
# locで置き場所を指定できる
# defaultの場所は自動的に決まる
ax.legend(fontsize=20) #,loc="lower right")
# xy軸のlabelを設定
ax.set_xlabel(L"E") #, size=20)
ax.set_ylabel(L"f") #, size=20)
# pdfで保存
# open("$(fname).pdf", "w") do io
# show(io, "application/pdf", fig)
# end
# epsで保存
# The PostScript backend does not support transparency; partially transparent artists will be rendered opaque.
#open("$(fname).eps", "w") do io
# show(io, "application/postscript", fig)
#end
# pngで保存
# open("$(fname).png", "w") do io
# show(io, "image/png", fig)
# end
# interactive modeで表示
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment