在3D Matplotlib图上调整网格线图
我正在为演示文稿准备,我有一些3D matplotlib数字的示例图。然而,网格线太轻以至于看不到投影图像。 在3D Matplotlib图上调整网格线图
我尝试使用网格的方法,对于2D人物的作品:
points = (5*np.random.randn(3, 50)+np.tile(np.arange(1,51), (3, 1))).transpose()
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(points[:,0], points[:,1], points[:,2])
ax.view_init(elev=0., azim=0)
ax.set_ylim([0, 60])
ax.set_zlim([0, 60])
ax.set_xlim([0, 60])
ax.set_zlabel('Cytokine')
ax.set_ylabel('Parameter')
ax.grid(linewidth=20)
但是,这似乎并没有对3D图形工作。有什么建议么?
不幸的是,这似乎并未公开。查看源代码,关键的内部变量是调用_AXINFO
,我们可以通过仔细的子类来覆盖。
添加该代码创建你的身材后,和风格也与字典custom_AXINFO
:
from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d.axis3d as axis3d
# New axis settings
custom_AXINFO = {
'x': {'i': 0, 'tickdir': 1, 'juggled': (1, 0, 2),
'color': (0.00, 0.00, 0.25, .75)},
'y': {'i': 1, 'tickdir': 0, 'juggled': (0, 1, 2),
'color': (0.20, 0.90, 0.90, 0.25)},
'z': {'i': 2, 'tickdir': 0, 'juggled': (0, 2, 1),
'color': (0.925, 0.125, 0.90, 0.25)},}
class custom_XAxis(axis3d.Axis):
_AXINFO = custom_AXINFO
class custom_YAxis(axis3d.Axis):
_AXINFO = custom_AXINFO
class custom_ZAxis(axis3d.Axis):
_AXINFO = custom_AXINFO
class custom_Axes3D(Axes3D):
def _init_axis(self):
'''Init 3D axes; overrides creation of regular X/Y axes'''
self.w_xaxis = custom_XAxis('x', self.xy_viewLim.intervalx,
self.xy_dataLim.intervalx, self)
self.xaxis = self.w_xaxis
self.w_yaxis = custom_YAxis('y', self.xy_viewLim.intervaly,
self.xy_dataLim.intervaly, self)
self.yaxis = self.w_yaxis
self.w_zaxis = custom_ZAxis('z', self.zz_viewLim.intervalx,
self.zz_dataLim.intervalx, self)
self.zaxis = self.w_zaxis
for ax in self.xaxis, self.yaxis, self.zaxis:
ax.init3d()
# The rest of your code below, note the call to our new custom_Axes3D
points = (5*np.random.randn(3, 50)+np.tile(np.arange(1,51), (3, 1))).transpose()
fig = plt.figure(figsize = (10,10))
ax = custom_Axes3D(fig)
这是猴子修补它是最差的,并且不应被依赖为更高版本工作。
固定面部颜色比网格线更容易,因为这需要覆盖一个__init__
方法,尽管它可以用更多的工作完成。
将这件事暴露给最终用户似乎并不困难,因此我可以想象这可能会在以后的版本中得到修复。
如果您不介意让所有的线条变粗,那么您可以调整默认的rc设置。
给定图,如:
我们可以添加:
import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 2
为了提高全行的默认行宽,给人一种结果:
Altern atively,如果你觉得这个长相丑陋,你可以使用:
ax.w_xaxis.gridlines.set_lw(3.0)
ax.w_yaxis.gridlines.set_lw(3.0)
ax.w_zaxis.gridlines.set_lw(3.0)
各轴的线宽度调整到3.0,产生:
为了更新颜色,所以网格线真正流行,你可以添加:
ax.w_xaxis._axinfo.update({'grid' : {'color': (0, 0, 0, 1)}})
ax.w_yaxis._axinfo.update({'grid' : {'color': (0, 0, 0, 1)}})
ax.w_zaxis._axinfo.update({'grid' : {'color': (0, 0, 0, 1)}})
主要生产:
这些方法非常不方便,但据我所知,没有简单的方法来实现这些结果!希望这可以帮助;让我知道如果你需要任何进一步的援助!
@JudoWill - 如果它解决了你的问题,你应该接受这个答案! – Jamie
它看起来像现在,你可以做ax.w_xaxis.plane.set_color((1,0,0)); ax.w_xaxis.plane.set_alpha(0.5)。它看起来像忽略了set_color中的alpha通道,但使用了set_alpha中的通道。 – Ben