4個のプロット作成
Python ソース
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(facecolor='greenyellow')
# ----- 第1プロット(左上) -----
x1 = np.linspace(-10, 10, 64)
y1 = np.sin(x1)/x1
ax1 = fig.add_subplot(2,2,1)
ax1.set_xlabel("x", fontsize=12, fontname='serif')
ax1.set_ylabel("y", fontsize=12, fontname='serif')
ax1.tick_params(axis='both', length=10, which='major')
ax1.tick_params(axis='both', length=5, which='minor')
ax1.set_xlim([-10, 10])
ax1.set_ylim([-1.2,1.2])
ax1.minorticks_on()
ax1.plot(x1, y1, 'ro', markersize=5)
# ----- 第2プロット(右上) -----
fourier = lambda x,n: 0.5*np.sin(n*x)/n+fourier(x,n-1) \
if 1<=n else 0
x2 = np.linspace(-10, 10, 512)
y2 = fourier(x2, 10)
ax2 = fig.add_subplot(2,2,2)
ax2.set_xlabel("x", fontsize=12, fontname='serif')
ax2.set_ylabel("y", fontsize=12, fontname='serif')
ax2.tick_params(axis='both', length=10, which='major')
ax2.tick_params(axis='both', length=5, which='minor')
ax2.set_xlim([-10, 10])
ax2.set_ylim([-1.2,1.2])
ax2.minorticks_on()
ax2.plot(x2, y2, 'b-', markersize=5)
# ----- 第3プロット(左下) -----
t = np.linspace(-np.pi, np.pi, 512)
x3 = np.sin(t)
y3 = np.cos(t+np.pi/4)
ax3 = fig.add_subplot(2,2,3)
ax3.set_xlabel("x", fontsize=12, fontname='serif')
ax3.set_ylabel("y", fontsize=12, fontname='serif')
ax3.tick_params(axis='both', length=10, which='major')
ax3.tick_params(axis='both', length=5, which='minor')
ax3.set_xlim([-1.2, 1.2])
ax3.set_ylim([-1.2,1.2])
ax3.minorticks_on()
ax3.plot(x3, y3, 'b-', markersize=5)
# ----- 第4プロット(右下) -----
x4, y4 = np.meshgrid(np.linspace(-2, 2, 32), np.linspace(-2, 2, 32))
z4 = 4*x4*np.exp(-(x4**2+y4**2))
ax4 = fig.add_subplot(2,2,4, projection='3d')
ax4.set_xlabel("x", fontsize=12, fontname='serif')
ax4.set_ylabel("y", fontsize=12, fontname='serif')
ax4.set_zlabel("z", fontsize=12, fontname='serif')
ax4.tick_params(axis='both', length=10, which='major')
ax4.tick_params(axis='both', length=5, which='minor')
ax4.set_xlim([-2, 2])
ax4.set_ylim([-2, 2])
ax4.set_zlim([-1.2, 1.2])
ax4.minorticks_on()
ax4.plot_surface(x4, y4, z4, cmap="plasma", rstride=1, cstride=1,
linewidth=0.5, edgecolors='black')
# ----- スクリーン表示 -----
fig.tight_layout()
plt.show()
# ----- 図のファイルの作成 -----
fig.savefig('plot4.pdf', orientation='portrait', \
transparent=False, bbox_inches=None, frameon=True)
fig.savefig('plot4.svg', orientation='portrait', \
transparent=False, bbox_inches=None, frameon=True)
fig.clf()
実行結果