使用Matplotlib进行3D绘图:隐藏轴但保留轴标签吗? [英] 3D Plot with Matplotlib: Hide axes but keep axis-labels?

查看:79
本文介绍了使用Matplotlib进行3D绘图:隐藏轴但保留轴标签吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Matplotlib可视化三维数组. 除了轻微的障碍外,我几乎以我想要的方式获得了它...请参见下面的插图和说明,以了解我可以做得到的事情以及我想要做的事情...

I am using Matplotlib to visualize three-dimensional arrays. I got it almost the way I want it, apart from a minor snag... see the illustration and the description below of what I can get it to do and what I want it to do...

  1. 显示一堆带有标签的立方体,以及一堆其他东西.
  2. 显示一堆立方体,但没有轴标签.
  3. 这是我想要的,但无法完成...我想显示一堆带有轴标签的多维数据集,但没有其他内容.

我希望你们能帮助我:)参见下面的资料.

I hope you guys can help me out :) See the source below.

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import rcParams
import numpy as np
rcParams['axes.labelsize'] = 14
rcParams['axes.titlesize'] = 16
rcParams['xtick.labelsize'] = 14
rcParams['ytick.labelsize'] = 14
rcParams['legend.fontsize'] = 14
rcParams['font.family'] = 'serif'
rcParams['font.serif'] = ['Computer Modern Roman']
rcParams['text.usetex'] = True
rcParams['grid.alpha'] = 0.0

def make_cube():
    """ A Cube consists of a bunch of planes..."""

    planes = {
        "top"    : ( [[0,1],[0,1]], [[0,0],[1,1]], [[1,1],[1,1]] ),
        "bottom" : ( [[0,1],[0,1]], [[0,0],[1,1]], [[0,0],[0,0]] ),
        "left"   : ( [[0,0],[0,0]], [[0,1],[0,1]], [[0,0],[1,1]] ),
        "right"  : ( [[1,1],[1,1]], [[0,1],[0,1]], [[0,0],[1,1]] ),
        "front"  : ( [[0,1],[0,1]], [[0,0],[0,0]], [[0,0],[1,1]] ),
        "back"   : ( [[0,1],[0,1]], [[1,1],[1,1]], [[0,0],[1,1]] )
    }
    return planes

def render_array(ary, highlight):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    cube = make_cube()

    for space in xrange(0, ary.shape[0]):
        for column in xrange(0, ary.shape[1]):
            for row in xrange(0, ary.shape[2]):
                alpha = 0.01
                if highlight[space,column,row] == 1:
                    alpha = 1
                for side in cube:
                    (Xs, Ys, Zs) = (
                        np.asarray(cube[side][0])+space+space*0.2,
                        np.asarray(cube[side][2])+row+row*0.2,
                        np.asarray(cube[side][3])+column+column*0.2
                    )
                    ax.plot_surface(Xs, Ys, Zs, rstride=1, cstride=1, alpha=alpha)

    highest = 0                         # Make it look cubic
    for size in ary.shape:
        if size > highest:
            highest = size
    ax.set_xlim((0,highest))
    ax.set_ylim((0,highest))
    ax.set_zlim((0,highest))


    ax.set_xlabel('Third dimension' )   # Meant to visualize ROW-MAJOR ordering 
    ax.set_ylabel('Row(s)')
    ax.set_zlabel('Column(s)')

    #plt.axis('off')    # This also removes the axis labels... i want those...
    #ax.set_axis_off()  # this removes too much (also the labels)

    # So I try this instead...
    ax.set_xticks([])          # removes the ticks... great now the rest of it
    ax.set_yticks([])
    ax.set_zticks([])
    #ax.grid(False)             # this does nothing....
    #ax.set_frame_on(False)     # this does nothing....
    plt.show()

def main():

    subject = np.ones((3,4,3))

    highlight = np.zeros(subject.shape) # Highlight a row
    highlight[1,1,:] = 1

    render_array(subject, highlight)    # Show it

if __name__ == "__main__":
    main()

更新,多亏了答案,这就是我所缺少的:

Update, thanks to the answer, here is what I was missing:

# Get rid of the panes                          
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 
ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0)) 

# Get rid of the spines                         
ax.w_xaxis.line.set_color((1.0, 1.0, 1.0, 0.0)) 
ax.w_yaxis.line.set_color((1.0, 1.0, 1.0, 0.0)) 
ax.w_zaxis.line.set_color((1.0, 1.0, 1.0, 0.0))

哪个会与:

# Get rid of the ticks                          
ax.set_xticks([])                               
ax.set_yticks([])                               
ax.set_zticks([])

隐藏标签以外的所有内容,如3)所示.

Hide everything but the labels, as illustrated in 3).

更新

我已经清理并使代码进入工作状态,并在此处提供了该代码: https://github.com/safl/ndarray_plot

I've cleaned and got the code into a working state and made it available here: https://github.com/safl/ndarray_plot

这里还有一些其他示例: http://nbviewer.ipython.org/github/safl /ndarray_plot/blob/master/nb/ndap.ipynb

Along with a couple of additional examples here: http://nbviewer.ipython.org/github/safl/ndarray_plot/blob/master/nb/ndap.ipynb

推荐答案

我尝试运行您的代码,但是在我的计算机上无法运行.

I tried to run your code, but it is not working in my computer.

在下面,您可以看到绘制球体的解决方案.基本上,我用手将窗格和刺的颜色变为alpha=0,并使刻度线成为空列表(如您所指出的那样).

Below you can see a solution for plotting a sphere. Basically, I turned the color of the panes and spines to alpha=0 by hand and make the ticks to be empty lists (as you pointed out).

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import rcParams
import numpy as np
rcParams['axes.labelsize'] = 18
rcParams['font.family'] = 'serif'
rcParams['font.serif'] = ['Computer Modern Roman']
rcParams['text.usetex'] = True

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)

x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z,  rstride=4, cstride=4, color='b')

# Get rid of the panes
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))

# Get rid of the spines
ax.w_xaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
ax.w_yaxis.line.set_color((1.0, 1.0, 1.0, 0.0))
ax.w_zaxis.line.set_color((1.0, 1.0, 1.0, 0.0))

# Get rid of the ticks
ax.set_xticks([]) 
ax.set_yticks([]) 
ax.set_zticks([])

# Add the labels
ax.set_xlabel('Third dimension' )
ax.set_ylabel('Row(s)')
ax.set_zlabel('Column(s)')
plt.show()

这是我的输出

这篇关于使用Matplotlib进行3D绘图:隐藏轴但保留轴标签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆