2017-04-15 89 views
1

是否可以将颜色图(现在位于原始图的右侧)放在图的顶部?重新定位颜色条

我的代码:

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data 
from matplotlib import cm 
import numpy as np 


# set up a figure twice as wide as it is tall 
fig = plt.figure(figsize=plt.figaspect(0.5)) 

#=============== 
# First subplot 
#=============== 
# set up the axes for the first plot 
ax = fig.add_subplot(1, 2, 1, projection='3d') 

# plot a 3D surface like in the example mplot3d/surface3d_demo 

X = np.arange(-5, 5, 0.25) 
Y = np.arange(-5, 5, 0.25) 
X, Y = np.meshgrid(X, Y) 
R = np.sqrt(X**2 + Y**2) 
Z = np.sin(R) 
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, 
         linewidth=0, antialiased=False) 
ax.set_zlim(-1.01, 1.01) 
fig.colorbar(surf, shrink=0.5, aspect=10) 
fig.savefig('64bit.png') 

回答

1

您必须添加附加轴(add_axes)把你的彩条在所需位置:

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data 
from matplotlib import cm 
from mpl_toolkits.axes_grid1 import make_axes_locatable 
import numpy as np 


# set up a figure twice as wide as it is tall 
fig = plt.figure(figsize=plt.figaspect(0.5)) 

#=============== 
# First subplot 
#=============== 
# set up the axes for the first plot 
ax = fig.add_subplot(1, 2, 1, projection='3d') 

# plot a 3D surface like in the example mplot3d/surface3d_demo 

X = np.arange(-5, 5, 0.25) 
Y = np.arange(-5, 5, 0.25) 
X, Y = np.meshgrid(X, Y) 
R = np.sqrt(X**2 + Y**2) 
Z = np.sin(R) 
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, 
         linewidth=0, antialiased=False) 
ax.set_zlim(-1.01, 1.01) 

# position of colorbar 
# where arg is [left, bottom, width, height] 
cax = fig.add_axes([0.15, .87, 0.35, 0.03]) 
fig.colorbar(surf, orientation='horizontal', cax=cax) 
plt.show() 

enter image description here

2

是的,有多个答案在这里在网站上显示你如何移动彩条周围像这样的:positioning the colorbar

在你的情况,你希望将其与orientation论点结合起来。据我所知,没有简单的方法将颜色条自动放置到图形的顶部,您将不得不手动放置它。这里是我的代码,取代您的fig.colorbar(surf, shrink=0.5, aspect=10)

cbax = fig.add_axes([0.1, 0.89, 0.5, 0.05]) 
fig.colorbar(surf, orientation="horizontal", cax=cbax) 

在列表中的数字描述了彩条的一些特点,如我所附着对方回答所提到的[left, bottom, width, height]

这些数字很适合您的情节,请随意将它们更改为您的喜好。

0

为了得到对剧情的顶部的彩条,你需要创建一些轴,指定为托管颜色条。 这既可以通过在图中的坐标一些给定位置放置新的轴手动完成,

cax = fig.add_axes([0.2,0.8,0.3,.05]) 
fig.colorbar(surf, cax=cax, orientation="horizontal") 

,或者通过使用副区格(gridspec),其在以下示出:

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d.axes3d import Axes3D 
import matplotlib.gridspec as gridspec 
import numpy as np 

x = np.arange(-5, 5, 0.25) 
X, Y = np.meshgrid(x,x) 
Z = np.sin(np.sqrt(X**2 + Y**2)) 

gs = gridspec.GridSpec(2, 2, height_ratios=[0.05,1]) 
fig = plt.figure() 
ax = fig.add_subplot(gs[1,0], projection='3d') 
cax = fig.add_subplot(gs[0,0]) 

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap="coolwarm", 
         linewidth=0, antialiased=False, vmin=-1, vmax=1) 

fig.colorbar(surf, cax=cax, orientation="horizontal", ticks=[-1,0,1]) 

plt.show() 

enter image description here