Set the title, axis label, etc. for the coordinate system (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()
fontdict
¶# 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()
set_position
¶The boundaries of the four corners (left, top, right, bottom) of the graph area represented by the Axes object are managed as objects of the Spine class.
Since
left
represents y-axis and bottom
represents the x-axis, each axis can be moved by changing the value of left
and bottom
key.
# 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()
Axes.set_xlim
, Axes.set_ylim
)¶Invert the x-axis and y-axis.
Axes.set_xlim(
$a$,
$b$)
: Set the x-axis range to [$a$, $b$]. The x-axis is inverted when $a>b$.Axes.set_ylim(
$a$,
$b$)
: Set the y-axis range to [$a$, $b$]. The y-axis is inverted when $a>b$.# 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()