座標系 (Axes) に対して、タイトルや軸ラベルなどを設定する。
# sample code 4-1-1
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = np.array([0, 1, 2, 3, 4])
ys = xs **2
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys)
ax.set_title('Center Title', color='blue', size=20, style='italic', family='fantasy', backgroundcolor='gold')
ax.set_title('Left Title', loc='left', color='green')
ax.set_title('Right Title', loc='right', color='red')
ax.set_xlabel('x-label', color='black', size=12)
ax.set_ylabel('y-label', color='orange', size=14, style='italic')
plt.show()
# sample code 4-1-2
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = np.array([0, 1, 2, 3, 4])
ys = xs **2
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys)
ax.set_title('Center Title', color='blue', size=20, style='italic', backgroundcolor='gold', position=(0.5, 1.1))
plt.show()
# sample code 4-1-3
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = np.array([0, 1, 2, 3, 4])
ys = xs **2
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys)
ax.set_xlabel('x-label', color='black', size=12, position=(1, 0), rotation=90)
ax.set_ylabel('y-label', color='orange', size=14, position=(0, 1), rotation=0)
plt.show()
# sample code 4-2
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
font = {
'family':'serif',
'color':'blue',
'style':'italic',
'weight':'bold',
'size':12,
'rotation': 0
}
xs = np.array([0, 1, 2, 3, 4])
ys = xs **2
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, ys)
ax.set_title('Center Title', fontdict=font)
ax.set_xlabel('x-label', fontdict=font)
ax.set_ylabel('y-label', fontdict=font)
plt.show()
# sample code 4-3
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
ys = [5.4, 8.5, 12.8, 15.1, 19.5, 23.2, 24.3, 29.1, 24.2, 17.5, 14.0, 7.7]
xticks = xs
xticklabels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
yticks = [0, 5, 10, 15, 20, 25, 30]
yticklabels = ['$0^{\circ}$', '$0^{\circ}$', '$10^{\circ}$', '$15^{\circ}$', '$20^{\circ}$', '$25^{\circ}$', '$30^{\circ}$']
fig, ax = plt.subplots(1, 1, figsize=(12,6))
ax.plot(xs, ys, marker='v')
ax.set_title('Average Temperature', fontdict=font)
ax.set_xlabel('Month')
ax.set_ylabel('Celsius')
ax.set_xticks(xticks)
ax.set_yticks(yticks)
ax.set_xticklabels(xticklabels)
ax.set_yticklabels(yticklabels)
plt.show()
Axesオブジェクトで表されるグラフアリアの四隅の境界( left, top, right, bottom) は Spineクラスのオブジェクトとして管理されている。
leftがy軸、bottomがx軸 をあらわすので、left , bottom の位置を移動することで、軸を移動することができる。
# sample code 4-4-1
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = np.linspace(-np.pi, np.pi, 100)
c = np.cos(xs)
s = np.sin(xs)
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, c, label='$y=cos(x)$')
ax.plot(xs, s, label='$y=sin(x)$')
ax.legend()
plt.show()
# sample code 4-4-2
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = np.linspace(-np.pi, np.pi, 100)
c = np.cos(xs)
s = np.sin(xs)
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, c, label='$y=cos(x)$')
ax.plot(xs, s, label='$y=sin(x)$')
ax.legend()
ax.spines['right'].set_color('none') # remove
ax.spines['top'].set_color('none') # remove
ax.spines['bottom'].set_position(('data',0)) # move x-axis to the origin point
ax.spines['left'].set_position(('data',0)) # move y-axis to the origin point
plt.show()
# sample code 4-4-3
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
xs = np.linspace(-np.pi, np.pi, 100)
c = np.cos(xs)
s = np.sin(xs)
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(xs, c, label='$y=cos(x)$')
ax.plot(xs, s, label='$y=sin(x)$')
ax.legend()
ax.spines['right'].set_color('none') # remove
ax.spines['top'].set_color('none') # remove
ax.spines['bottom'].set_position(('data',0)) # move x-axis to the origin point
ax.spines['left'].set_position(('data',0)) # move y-axis to the origin point
#ax.xaxis.set_ticks_position('bottom') # move xticks to the new x-axis position
#ax.yaxis.set_ticks_position('left') # move yticks to the new y-axis position
ax.set_xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
ax.set_xticklabels(['$-\pi$', '$-\pi/2$', '$0$', '$\pi/2$', '$\pi$'])
ax.set_yticks([-1, 0, 1])
ax.set_yticklabels(['$-1$', '$0$', '$1$'])
plt.show()
x軸やy軸を反転する。
# sample code 4-5-1
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0.01, 5.0, 0.01)
y = np.exp(-x)
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.plot(x, y)
ax.grid(True)
plt.show()
# sample code 4-5-2
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0.01, 5.0, 0.01)
y = np.exp(-x)
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.set_xlim(5, 0) # invert x-axis
ax.plot(x, y)
ax.grid(True)
plt.show()
# sample code 4-5-3
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0.01, 5.0, 0.01)
y = np.exp(-x)
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.set_ylim(1, 0) # invert y-axis
ax.plot(x, y)
ax.grid(True)
plt.show()