设置Matplotlib 3D图的刻度颜色 [英] Setting tick colors of matplotlib 3D plot

查看:119
本文介绍了设置Matplotlib 3D图的刻度颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有3D matplotlib图(Axes3D对象),如何更改刻度线的颜色?我想出了如何更改轴线,刻度线标签和轴线标签的颜色的方法.显而易见的解决方案是使用ax.tick_params(axis='x', colors='red'),仅更改刻度线标签,而不更改刻度线本身.

If I have a 3D matplotlib plot (Axes3D object), how do I change the color of the tick marks? I figured out how to change the color of the axis line, the tick labels, and the axis label. The obvious solution, use ax.tick_params(axis='x', colors='red'), only changes the tick labels not the tick marks themselves.

这是试图将所有轴更改为红色并获取除勾号之外的所有内容的代码:

Here is code that tries to change all the axes to red and gets everything but the tick marks:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt

fig = plt.figure()
ax = Axes3D(fig)

ax.scatter((0, 0, 1), (0, 1, 0), (1, 0, 0))
ax.w_xaxis.line.set_color('red')
ax.w_yaxis.line.set_color('red')
ax.w_zaxis.line.set_color('red')
ax.w_zaxis.line.set_color('red')
ax.xaxis.label.set_color('red')
ax.yaxis.label.set_color('red')
ax.zaxis.label.set_color('red')
ax.tick_params(axis='x', colors='red')  # only affects
ax.tick_params(axis='y', colors='red')  # tick labels
ax.tick_params(axis='z', colors='red')  # not tick marks

fig.show()

推荐答案

As mentioned in manual page about tick_params(axis='both', **kwargs) you get a bug:

虽然当前已实现此功能,但该功能的核心部分 Axes3D对象可能会忽略其中一些设置.将来的版本将 解决这个问题.提交错误的人将得到优先考虑.

While this function is currently implemented, the core part of the Axes3D object may ignore some of these settings. Future releases will fix this. Priority will be given to those who file bugs.

要解决此问题,请使用内部_axinfo词典,如此示例:

To override this problem use inner _axinfo dictionary as in this example:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.scatter((0, 0, 1), (0, 1, 0), (1, 0, 0))

ax.xaxis._axinfo['tick']['color']='r'
ax.yaxis._axinfo['tick']['color']='r'
ax.zaxis._axinfo['tick']['color']='r'
plt.show()

这篇关于设置Matplotlib 3D图的刻度颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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