在matplotlib图的子图中移动单个轴

时间:2016-11-16 22:05:45

标签: python matplotlib axes mplot3d

我有一个带有四个子图的python matplotlib图。如果我单独绘制各个轴,它们在画布上很合适。但由于某些原因,当他们处于子图中时,它们会向右移动很远,并且会切断一些标签。我怎样才能保持所有尺寸的缩放,但是只需将四个轴中的每一个向左移动一点?如果有帮助,我的代码如下。

我会附上我正在获得的人物形象。我只是注意到这个特殊的数字没有任何标签被切断,因为它们很短。 (这里我只在第一个子图中有标签。)但是当我使用长标签时它们会被切断。

Figure

我在网上做了很多搜索,但尚未确定答案。希望事情容易。

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

@np.vectorize
def ImbalPenSQ(ImbalPercent, IncCost):

    if np.abs(ImbalPercent) <= 1.5:
        TieredPen = 0
    elif 1.5 < np.abs(ImbalPercent) <= 7.5:
        TieredPen = 0.10
    else:
        TieredPen = 0.25

    PenFloor = 10

    Penalty = TieredPen * IncCost

    return Penalty


@np.vectorize
def ImbalPS(ImbalPercent, IncCost):

    if np.abs(ImbalPercent) <= 1.5:
        TieredPen = 0
    elif 1.5 < np.abs(ImbalPercent) <= 7.5:
        TieredPen = 0.10
    else:
        TieredPen = 0.25

    PenFloor = 10

    Penalty = TieredPen * np.abs(IncCost)

    return Penalty

@np.vectorize
def Simple(ImbalPercent, IncCost):

    if np.abs(ImbalPercent) <= 1.5:
        TieredPen = 0
    elif 1.5 < np.abs(ImbalPercent) <= 7.5:
        TieredPen = 0.10
    else:
        TieredPen = 0.25

    PenFloor1 = 10
    PenFloor2 = 20

    Penalty = TieredPen * np.abs(IncCost)

    if np.sign(ImbalPercent) == np.sign(IncCost):
        Penalty = 0

    if TieredPen == 0.10 and Penalty < PenFloor1:
        Penalty = PenFloor1
    elif TieredPen == 0.25 and Penalty < PenFloor2:
        Penalty = PenFloor2

    return Penalty

@np.vectorize
def Smooth(ImbalPercent, IncCost):

    if np.abs(ImbalPercent) <= 1.5:
        Penalty = 0
    else:
        Penalty = 3*(np.abs(ImbalPercent)-1.5)

    if (np.sign(ImbalPercent) != np.sign(IncCost)) and np.abs(ImbalPercent) >= 1.5:
        Penalty = Penalty + 0.02*np.abs(IncCost)*(np.abs(ImbalPercent)-1.5)

    return Penalty


# Set up general layout of figure
fig = plt.figure(figsize=(20,20))

my_elev = 36
my_azim = -59

my_zmin = -80.
my_zmax = 160.

my_norm = mpl.colors.Normalize(vmin=my_zmin,vmax=my_zmax)

my_cmap = cm.cubehelix


# X is vector of imbalance percentages
X = np.arange(-20,20,.2)
# Y is vector of incremental costs
Y = np.arange(-200,500,5)
X, Y = np.meshgrid(X, Y)


# Create status quo axis
ax1 = fig.add_subplot(2,2,1, projection='3d')

Z = ImbalPenSQ(X,Y)
surf1 = ax1.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=my_cmap, norm=my_norm, linewidth=0, antialiased=False)
ax1.set_zlim(my_zmin,my_zmax)

ax1.zaxis.set_major_locator(LinearLocator(7))
ax1.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

ax1.set_xlabel('imbalance %', labelpad=10)
ax1.set_ylabel('incremental cost', labelpad=10)
ax1.set_zlabel('effective penalty', labelpad=20)
ax1.set_title('Status quo')
ax1.view_init(elev=my_elev, azim=my_azim)

ax1.tick_params(pad=5)
ax1.tick_params(axis='z',pad=15)

# Create PS policy axis
ax2 = fig.add_subplot(2,2,2, projection='3d')

Z = ImbalPS(X,Y)
surf2 = ax2.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=my_cmap, norm=my_norm, linewidth=0, antialiased=False)
ax2.set_zlim(my_zmin,my_zmax)

ax2.zaxis.set_major_locator(LinearLocator(7))
ax2.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

ax2.set_title('PS policy')
ax2.view_init(elev=my_elev, azim=my_azim)

ax2.tick_params(pad=5)
ax2.tick_params(axis='z',pad=15)
ax2.tick_params(labelcolor='w',labelsize=0)



# Create simple alternative policy axis
ax3 = fig.add_subplot(2,2,3, projection='3d')

Z = Simple(X,Y)
surf3 = ax3.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=my_cmap, norm=my_norm, linewidth=0, antialiased=False)
ax3.set_zlim(my_zmin,my_zmax)

ax3.zaxis.set_major_locator(LinearLocator(7))
ax3.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

ax3.set_title('Simple alternative policy')
ax3.view_init(elev=my_elev, azim=my_azim)

ax3.tick_params(pad=5)
ax3.tick_params(axis='z',pad=15)
ax3.tick_params(labelcolor='w',labelsize=0)

# Create amooth alternative policy axis
ax4 = fig.add_subplot(2,2,4, projection='3d')

Z = Smooth(X,Y)
surf4 = ax4.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=my_cmap, norm=my_norm, linewidth=0, antialiased=False)
ax4.set_zlim(my_zmin,my_zmax)

ax4.zaxis.set_major_locator(LinearLocator(7))
ax4.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

ax4.set_title('Smooth alternative policy')
ax4.view_init(elev=my_elev, azim=my_azim)

ax4.tick_params(pad=5)
ax4.tick_params(axis='z',pad=15)
ax4.tick_params(labelcolor='w',labelsize=0)



plt.tight_layout()

plt.show()

0 个答案:

没有答案