Matplotlib 3D:删除轴刻度并绘制上边缘边框?

pxyaymoc  于 2023-08-06  发布在  其他
关注(0)|答案(3)|浏览(187)

看起来有些方法在matplotlib 2D中有效,但在matplotlib 3D中可能无效。我不确定
我想删除所有轴上的刻度线,并将边缘颜色从底部和侧面扩展到顶部。我得到的最远的是能够将刻度绘制为白色,这看起来很糟糕,因为它们被渲染在边缘线的顶部。
下面是一大块自包含的代码,导致了下面的图像。任何帮助是非常感谢!

import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D

mpl.rcParams['ytick.color'] = 'white'
#mpl.rcParams['ytick.left'] = False

sample = np.random.random_integers(low=1,high=5, size=(10,3))

# Create a figure and a 3D Axes
fig = plt.figure(figsize=(5,5))

ax = Axes3D(fig)

#ax.w_xaxis.set_tick_params(color='white')

#ax.axes.tick_params
ax.axes.tick_params(bottom=False, color='blue')
##['size', 'width', 'color', 'tickdir', 'pad', 'labelsize', 
##'labelcolor', 'zorder', 'gridOn', 'tick1On', 'tick2On', 
##'label1On', 'label2On', 'length', 'direction', 'left', 'bottom', 
##'right', 'top', 'labelleft', 'labelbottom', 
##'labelright', 'labeltop', 'labelrotation']

colors = np.mean(sample[:, :], axis=1)

ax.scatter(sample[:,0], sample[:,1], sample[:,2],

           marker='o', s=20, c=colors, alpha=1)

ax.tick_params(color='red')

frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.zaxis.set_ticklabels([])
#frame1.axes.yaxis.set_tick_params(color='white')

字符串
x1c 0d1x的数据

oipij1gg

oipij1gg1#

要回答问题的第一部分,关于去除刻度,最简单的方法可能是禁用刻度线:

for line in ax.xaxis.get_ticklines():
    line.set_visible(False)
for line in ax.yaxis.get_ticklines():
    line.set_visible(False)
for line in ax.zaxis.get_ticklines():
    line.set_visible(False)

字符串
例如:

import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D

sample = np.random.random_integers(low=1,high=5, size=(10,3))

# Create a figure and a 3D Axes
fig = plt.figure(figsize=(5,5))

ax = Axes3D(fig)

colors = np.mean(sample[:, :], axis=1)

ax.scatter(sample[:,0], sample[:,1], sample[:,2],

           marker='o', s=20, c=colors, alpha=1)

ax = plt.gca()
ax.xaxis.set_ticklabels([])
ax.yaxis.set_ticklabels([])
ax.zaxis.set_ticklabels([])

for line in ax.xaxis.get_ticklines():
    line.set_visible(False)
for line in ax.yaxis.get_ticklines():
    line.set_visible(False)
for line in ax.zaxis.get_ticklines():
    line.set_visible(False)


的数据

ev7lccsx

ev7lccsx2#

对于较新版本(例如matplotlib 3.5.1)很多格式化可以通过mpl_toolkits.mplot3d.axis3d._axinfo完成:

import numpy as np
from matplotlib import pyplot as plt

sample = np.random.randint(low=1,high=5, size=(10,3))
# Create a figure and a 3D Axes
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(projection='3d')

colors = np.mean(sample[:, :], axis=1)

ax.scatter(sample[:,0], sample[:,1], sample[:,2],
           marker='o', s=20, c=colors, alpha=1)

for axis in [ax.xaxis, ax.yaxis, ax.zaxis]:
    axis.set_ticklabels([])
    axis._axinfo['axisline']['linewidth'] = 1
    axis._axinfo['axisline']['color'] = (0, 0, 0)
    axis._axinfo['grid']['linewidth'] = 0.5
    axis._axinfo['grid']['linestyle'] = "-"
    axis._axinfo['grid']['color'] = (0, 0, 0)
    axis._axinfo['tick']['inward_factor'] = 0.0
    axis._axinfo['tick']['outward_factor'] = 0.0
    axis.set_pane_color((0.95, 0.95, 0.95))

plt.show()

字符串


的数据

7z5jn7bk

7z5jn7bk3#

这已经被添加到即将发布的matplotlib 3.8.0的一个特性中。参见:https://github.com/matplotlib/matplotlib/pull/25830

import matplotlib.pyplot as plt

positions = ['lower', 'upper', 'default', 'both', 'none']
fig, axs = plt.subplots(2, 3, figsize=(12, 8),
                        subplot_kw={'projection': '3d'})
for ax, pos in zip(axs.flatten(), positions):
    for axis in ax.xaxis, ax.yaxis, ax.zaxis:
        axis.set_label_position(pos)
        axis.set_ticks_position(pos)
    title = f'position="{pos}"'
    ax.set(xlabel='x', ylabel='y', zlabel='z', title=title)
axs[1, 2].axis('off')

字符串


的数据

相关问题