[python][matplotlib]陰関数の領域や条件分岐のあるグラフの図示

条件分岐のあるグラフの図示

コードは以下。

import pandas as pd
import numpy as np
from scipy import stats
from matplotlib import pylab as plt
import seaborn as sns
plt.style.use("seaborn-white")
import math
from sympy import *

#define the area to be plotted
max_range = 3
delta = 0.025 #delta
x = np.arange(-max_range, max_range, delta)
y = np.arange(-max_range, max_range, delta)
X, Y = np.meshgrid(x, y)
x1 = np.arange(-max_range, -1, delta)
y1 = np.arange(0, max_range, delta)
X1, Y1 = np.meshgrid(x1, y1)
x2 = np.arange(-1, 1, delta)
y2 = np.arange(-max_range, max_range, delta)
X2, Y2 = np.meshgrid(x2, y2)
x3 = np.arange(1, max_range, delta)
y3 = np.arange(0, max_range, delta)
X3, Y3 = np.meshgrid(x3, y3)

#create a figure
fig, ax = plt.subplots(1, 1)

#axis settings
plt.axis([-max_range, max_range, -max_range, max_range])
plt.gca().set_aspect('equal', adjustable='box')

#set the axis to the center
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.spines["bottom"].set_position(("data",0))
ax.yaxis.set_ticks_position("left")
ax.spines["left"].set_position(("data",0))

#label the axes
ax.set_ylabel(r"$y$", y = 1, rotation = 0, fontsize = 12, labelpad = -0.5)
ax.set_xlabel(r"$x$", x = 1, fontsize = 12)
ax.set_xticks([-3, -2, -1, 1, 2, 3])
ax.set_yticks([-3, -2, -1, 1, 2, 3])
ax.tick_params(axis = "x", which = "major", pad = -10)

#plot
Z = -X**2+Y**2+1
Z1 = -X1**2+Y1**2+1
Z2 = -X2**2+Y2**2+1
Z3 = -X3**2+Y3**2+1
plt.contour(X, Y, Z, [0], colors = "black", corner_mask = True)#plot the margins
plt.plot(X1[Z1 > 0], Y1[Z1 > 0], color = "gray", alpha = 0.5)
plt.plot(X2[Z2 > 0], Y2[Z2 > 0], color = "gray", alpha = 0.5)
plt.plot(X3[Z3 > 0], Y3[Z3 > 0], color = "gray", alpha = 0.5)
plt.axhline(0, linewidth = 2, color = "gray")
plt.savefig("/Users/Desktop/figure.png", dpi = 300)

出力されるグラフ

上記のコードで出力されるグラフ

コードの解説

以下で必要なLibraryをimport。wrapperとしてseabornを用いている。backgroundは”white”に。

import pandas as pd
import numpy as np
from scipy import stats
from matplotlib import pylab as plt
import seaborn as sns
plt.style.use("seaborn-white")
import math
from sympy import *

以下が工夫したところで、この部分で\(x\)の領域に対して、動きうる\(y\)の領域を予め規定している。つまり、\(x\)で条件分岐を指定して、その\(x\)に対して、\(y\)の領域は答えの領域になっている。例えば、\(x2\)が\(-1\)から\(1\)まで動く間、\(y\)はすべての実数を動くので、\(-max\_range < y < max\_range\)としている。

#define the area to be plotted
max_range = 3
delta = 0.025 #delta
x = np.arange(-max_range, max_range, delta)
y = np.arange(-max_range, max_range, delta)
X, Y = np.meshgrid(x, y)
x1 = np.arange(-max_range, -1, delta)
y1 = np.arange(0, max_range, delta)
X1, Y1 = np.meshgrid(x1, y1)
x2 = np.arange(-1, 1, delta)
y2 = np.arange(-max_range, max_range, delta)
X2, Y2 = np.meshgrid(x2, y2)
x3 = np.arange(1, max_range, delta)
y3 = np.arange(0, max_range, delta)
X3, Y3 = np.meshgrid(x3, y3)

以下は図やプロットの細かい設定。

#create a figure
fig, ax = plt.subplots(1, 1)

#axis settings
plt.axis([-max_range, max_range, -max_range, max_range])
plt.gca().set_aspect('equal', adjustable='box')

#set the axis to the center
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.spines["bottom"].set_position(("data",0))
ax.yaxis.set_ticks_position("left")
ax.spines["left"].set_position(("data",0))

#label the axes
ax.set_ylabel(r"$y$", y = 1, rotation = 0, fontsize = 12, labelpad = -0.5)
ax.set_xlabel(r"$x$", x = 1, fontsize = 12)
ax.set_xticks([-3, -2, -1, 1, 2, 3])
ax.set_yticks([-3, -2, -1, 1, 2, 3])
ax.tick_params(axis = "x", which = "major", pad = -10)

プロットではmatplotlibのcontourを用いている。等高線で、高さ\(0\)の部分に線を引きたいので、[0]と指定している。

#plot
Z = -X**2+Y**2+1
Z1 = -X1**2+Y1**2+1
Z2 = -X2**2+Y2**2+1
Z3 = -X3**2+Y3**2+1
plt.contour(X, Y, Z, [0], colors = "black", corner_mask = True)#plot the margins
plt.plot(X1[Z1 > 0], Y1[Z1 > 0], color = "gray", alpha = 0.5)
plt.plot(X2[Z2 > 0], Y2[Z2 > 0], color = "gray", alpha = 0.5)
plt.plot(X3[Z3 > 0], Y3[Z3 > 0], color = "gray", alpha = 0.5)
plt.axhline(0, linewidth = 2, color = "gray")
plt.savefig("/Users/Desktop/figure.png", dpi = 300)

関連リンク

2015年東京大学理系数学問題1
pythonでのグラフ、図の描写のまとめ-seabornでのグラフ作成方法を中心に-
seabornで細かい図の設定を調整する

コメント

タイトルとURLをコピーしました