実際、特に公式ドキュメントでは、記事内の内容については特に触れていませんが、関連するコードは表示されています。
Axesオブジェクトをインスタンス化した後、これを呼び出すことでAxesオブジェクトをインスタンス化することができます。
Axes.spines["top/left/bottom/right"]
で上、左、下、右の座標尾根インスタンスを取得します。
座標尾根のインスタンスを取得したら、関連するメソッドを使用して尾根の性質をパーソナライズできます。
set_visible は尾根が見えるかどうかを設定します。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator,FuncFormatter
x = np.linspace(0, 2 * np.pi, 100)
y = 2 * np.sin(x)
fig,ax=plt.subplots(figsize=(8,8))
# 軸のスタイルを設定する
# --------------------------------------------------------
def major_tick(x,pos):
if (x==0):
return "0"
if(x==np.pi):
return " "
else:
return "%d "%(x/3.14)
ax.xaxis.set_major_locator(MultipleLocator(np.pi))
ax.xaxis.set_major_formatter(FuncFormatter(major_tick))
# --------------------------------------------------------
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.plot(x,y)
plt.show()
set_position は軸の位置を設定します。
import numpy as np
import matplotlib.pyplot as pl
tfrom matplotlib.ticker import MultipleLocator,FuncFormatte
rx = np.linspace(0, 2 * np.pi, 100)
y = 2 * np.sin(x)
fig,ax=plt.subplots(figsize=(8,8))
# 軸のスタイルを設定する
# --------------------------------------------------------
def major_tick(x,pos):
if (x==0):
return "0"
if(x==np.pi):
return " "
else:
return "%d "%(x/3.14)
ax.xaxis.set_major_locator(MultipleLocator(np.pi))
ax.xaxis.set_major_formatter(FuncFormatter(major_tick))
# --------------------------------------------------------
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_position(("data",0))
ax.spines["bottom"].set_position(("axes",0.5))
ax.plot(x,y)
plt.show()
他にもいろいろな背骨の作り方がありますが、個人的に最も役に立つのは最後の2つだと思います。