WHen arranging multiple Axes
objects, the axis scale labels may overlap.
In such cases, the y-axis and x-axis scale labels can be shared for easy viewing.
# sample code 8-1-1
# share x-axis
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
y2 = np.sin(x * 2)
fig, ax = plt.subplots(2, 1, figsize=(8, 6), sharex=True)
ax[0].plot(x, y)
ax[1].scatter(x, y2)
plt.show()
# sample code 8-1-2
# share x-axis
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
y2 = np.sin(x) * 2
fig, ax = plt.subplots(1, 2, figsize=(8, 6), sharey=True)
ax[0].plot(x, y)
ax[1].scatter(x, y2)
plt.show()
# sample code 8-1-3
# share x-axis and y-axis
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
y2 = np.sin(x) * 2
y3 = np.cos(x)
y4 = np.cos(x) * 2
fig, ax = plt.subplots(2, 2, figsize=(8, 6), sharex=True, sharey=True)
ax[0][0].plot(x, y)
ax[0][1].plot(x, y2)
ax[1][0].plot(x, y3)
ax[1][1].plot(x, y4)
plt.show()
Sharing an axis between axes with different axis scales causes the problem that one of them is displayed smaller because the larger axis is adopted. In such a case, the sharing range of the axis is limited.
# sample code 8-2-1
# share x-axis for each row
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
y2 = np.sin(x) * 2
y3 = np.cos(x)
y4 = np.cos(x) * 2
fig, ax = plt.subplots(2, 2, figsize=(8, 6), sharex='col', sharey='col')
ax[0][0].plot(x, y)
ax[0][1].plot(x, y2)
ax[1][0].plot(x, y3)
ax[1][1].plot(x, y4)
plt.show()
# sample code 8-2-1
# share x-axis for each row
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 200)
x2 = np.linspace(0, 4*np.pi, 400)
y = np.sin(x)
y2 = np.cos(x)
y3 = np.sin(x2)
y4 = np.cos(x2)
fig, ax = plt.subplots(2, 2, figsize=(8, 6), sharex='row', sharey='row')
ax[0][0].plot(x, y)
ax[0][1].plot(x, y2)
ax[1][0].plot(x2, y3)
ax[1][1].plot(x2, y4)
plt.show()
When plotting a graph, it is drawn in the specified area, so it may be displayed vertically or horizontally even if the x-axis and y-axis scale are the same. There is a method to display the x-axis and y-axis on the same scale.
# sample code 8-3-1
# First, try drawing without specifying axis or set_aspect
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
theta = np.linspace(0, 2 * np.pi, 200)
x = 3 * np.cos(theta)
y = 3 * np.sin(theta)
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(x,y)
plt.show()
# sample code 8-3-3
# Specify axis('equal')
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
theta = np.linspace(0, 2 * np.pi, 200)
x = 3 * np.cos(theta)
y = 3 * np.sin(theta)
fig, ax = plt.subplots(1, 1, figsize=(8,6))
ax.plot(x,y)
ax.axis('equal')
plt.show()
# sample code 8-3-3
# Make the ratio of x-axis and y-axis the same
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
theta = np.linspace(0, 2 * np.pi, 200)
x = 3 * np.cos(theta)
y = 3 * np.sin(theta)
fig, axs = plt.subplots(2,3,figsize=(8,8)) # 2 lines, 3 columns
# Specify 'square' with axis() to scale as a square box.
axs[0][0].plot(x,y)
axs[0][0].axis('square')
axs[0][0].set_title('axis(square)',fontsize=10)
# Specify 'equal' with axis() to scale without changing the size of the box.
axs[0][1].plot(x, y)
axs[0][1].axis('equal')
axs[0][1].set_title('axis(equal)',fontsize=10)
# Specify 'scaled' with axis() to resize and scale the box.
axs[0][2].plot(x, y)
axs[0][2].axis('scaled')
axs[0][2].axis([-3,3,-3,3])
axs[0][2].set_title('axis(scaled)',fontsize=10)
# Specify 'equal' and 'box' with set_aspect() to resize the box.
axs[1][0].plot(x, y)
axs[1][0].set_aspect('equal','box')
axs[1][0].set_title('set_aspect(equal,box)',fontsize=10)
# Specify 'equal' and 'datalim' with set_aspect() without changing the box size.
axs[1][1].plot(x, y)
axs[1][1].set_aspect('equal','datalim')
axs[1][1].set_title('set_aspect(equal,datalim)',fontsize=10)
# Specify spect=1 with set_aspect() to set the ratio of x-axis to y-axis to 1:1.
axs[1][2].plot(x, y)
axs[1][2].set_aspect(aspect=1)
axs[1][2].set_title('set_aspect(aspect=1)',fontsize=10)
plt.show()
The x-axis and y-axis scaling is 'linear' by default, but you can change this to 'log', 'semilog', 'symlog', 'logit', and so on.
Try changing to logarithmic scaling.
x = np.arange(10)
print(x)
[0 1 2 3 4 5 6 7 8 9]
# sample code 8-4
# y軸をlinear, log, symlog, logit でスケーリングする
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-10, 10.1)
y = np.exp(x)
x2 = np.arange(0.1, 200, 0.1)
y2 = np.log(x2)
fig, ax = plt.subplots(2, 2, figsize=(8,8))
# linear
ax[0][0].plot(x, y)
ax[0][0].set_title('linear')
ax[0][0].grid(True)
# log
ax[0][1].plot(x, y)
ax[0][1].set_yscale('log')
ax[0][1].set_title('yscale(log)')
ax[0][1].grid(True)
# linear
ax[1][0].plot(x2, y2)
ax[1][0].set_title('linear')
ax[1][0].grid(True)
# log
ax[1][1].plot(x2, y2)
ax[1][1].set_xscale('log')
ax[1][1].set_title('xscale(log)')
ax[1][1].grid(True)
plt.show()
You may want to draw two data of different scales and compare them with each other. In this case, place an additional y-axis (Axes object) of a different scale on the opposite side (right).
# sample code 8-5-1
# Displaying data of different scales on a common y-axis cannot be expressed well.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.01, 10.0, 0.01)
y1 = np.exp(x)
y2 = np.sin(x)
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.plot(x, y1, color='blue')
ax.plot(x, y2, color='green')
plt.show()
# sample code 8-5-2
# Displaying data of different scales on the left and right y-axis
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.01, 10.0, 0.01)
y1 = np.exp(x)
y2 = np.sin(x)
fig, ax = plt.subplots(1, 1, figsize=(8, 6))
ax.plot(x, y1, color='blue')
ax.set_xlabel('x')
ax.set_ylabel('exp', color='blue')
ax.tick_params(axis='y', labelcolor='blue')
ax2 = ax.twinx()
ax2.plot(x, y2, color='green')
ax2.set_ylabel('sin', color='green')
ax2.tick_params(axis='y', labelcolor='green')
plt.show()