Python画两元一次分段函数

目的:如标题,观察变量之间的关系
代码分享:
#!/usr/bin/env python3
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

fig = plt.figure()
ax = fig.gca(projection=‘3d’)

Make data.

X = np.arange(0.5, 2, 0.1)
Y = np.arange(20, 200, 12)

Z = np.ones((len(X), len(Y)))
for i in range(len(X)):
for j in range(len(Y)):
# print(Y[j])
if 0 < (Y[j] - 12 * X[i]) and (Y[j] - 12 * X[i]) < 30:
Z[i, j] = 9.6 * X[i] + 6
if 30 <= (Y[j] - 12 * X[i]) and (Y[j] - 12 * X[i]) < 45:
Z[i, j] = 9.12 * X[i] + 0.24 * Y[j] - 1.2
if 45 <= (Y[j] - 12 * X[i]) and (Y[j] - 12 * X[i]) < 60:
Z[i, j] = 8.88 * X[i] + 0.26 * Y[j] - 2.1
if 60 <= (Y[j] - 12 * X[i]):
Z[i, j] = 8.4 * X[i] + 0.3 * Y[j] - 4.5

XX, YY = np.meshgrid(X, Y)

Plot the surface.

surf = ax.plot_surface(XX, YY, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=True)
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

你可能感兴趣的:(Python画两元一次分段函数)