如何更改matplotlib 3D图的轴的颜色? [英] How do I change the color of the axes of a matplotlib 3D plot?

查看:117
本文介绍了如何更改matplotlib 3D图的轴的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置

import matplotlib as mpl
AXES_COLOR = '#333333'
mpl.rc('axes', edgecolor=AXES_COLOR, labelcolor=AXES_COLOR, grid=True)
mpl.rc('xtick', color=AXES_COLOR)
mpl.rc('ytick', color=AXES_COLOR)
mpl.rc('grid', color=AXES_COLOR)

在2D和3D中都正确设置了轴标签的颜色和刻度.但是,edgecolor 不适用于 3D 轴并且它们保持黑色.同样,网格不受影响.

The color of the axes labels and the ticks are properly set both in 2D and in 3D. However, the edgecolor doesn't apply to 3D axes and they remain black. Likewise, the grid isn't affected.

我想知道如何访问 3D 绘图的各个轴:

I think figured out how to access the individual axes of a 3D plot:

import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d # Needed for 3d projection.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.w_zaxis # <- the z axis

文档 提到了一个属性在开发人员完成重构其3D代码之前,我们可以使用:

The documentation mentions a property that we can use until the developers have finished refactoring their 3D code:

import pprint
pprint.pprint(ax.w_xaxis._AXINFO)

{'x': {'color': (0.95, 0.95, 0.95, 0.5),
       'i': 0,
       'juggled': (1, 0, 2),
       'tickdir': 1},
 'y': {'color': (0.9, 0.9, 0.9, 0.5),
       'i': 1,
       'juggled': (0, 1, 2),
       'tickdir': 0},
 'z': {'color': (0.925, 0.925, 0.925, 0.5),
       'i': 2,
       'juggled': (0, 2, 1),
       'tickdir': 0}}

但是,color参数更改的是轴平面(在网格线之间)的背景颜色,而不是这些平面的边缘的颜色.

However, the color parameter changes the color of the background of the axes planes (between the wired of the grid), not the color of the edges of these planes.

我是不是挖得太深了?

推荐答案

事实证明这是不可能的,因为这些值是硬编码的.这封来自 matplotlib-users 邮件列表的存档电子邮件帮助了我.这是相关的部分:

Turns out it's impossible since these values are hard-coded. This archived email from the matplotlib-users mailing list helped me. Here's the relevant part:

不幸的是,您偶然发现了mplot3d的一种丑陋之处执行.我希望下一个可以使用更多的控件释放.但是现在,无法关闭轴刺(因为它们不是作为刺来实现的).如果您真的想深入了解源代码,您可以在其中将color参数更改为Line2D调用matplotlib/lib/mpl_toolkits/axis3d.py中的init3d()方法

Unfortunately, you have stumbled upon one of the ugliness of the mplot3d implementation. I am hoping to have more control available for the next release. But right now, there is no way to turn off the axes spines (because they aren't implemented as spines). If you really want to dig into the source code, you could change the color argument to the Line2D call in the init3d() method in matplotlib/lib/mpl_toolkits/axis3d.py

虽然这个答案解决了另一个问题,但它把我带到了 axis3d.py 的方向.我在/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d 中找到了它.我备份了原始的 axis3d.py ,然后将 axis3d.pyc 移开了.

Although this answer was addressing another concern, it sent me to the direction of axis3d.py. I found it in /usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d. I made a backup of the original axis3d.py and I moved axis3d.pyc away.

由于代码很短而且写得相当好,我很快就找到了我必须更改的两行.

Since the code is pretty short and fairly well written it didn't take long to locate the two lines I had to change.

  • 要更改各个轴的边缘颜色,我修改了 __ init __ 中的 self.line = ... :只需替换 color =(0、0、0、1),然后按 color =(1、0、0、1)表示可怕的红色.元组的成分是红色,绿色,蓝色,alpha,所有从0到1的浮动.
  • 要更改网格的颜色,我修改了 draw 方法.我用自己选择的颜色替换了 self.gridlines.set_color([[0.9,0.9,0.9,1)] * len(lines))的颜色.
  • To change the color of the edges of the individual axes, I modified the self.line=... in __init__: just replace color=(0, 0, 0, 1) by color=(1, 0, 0, 1) for a horribly flashy red. Components of the tuple are red, green, blue, alpha, all floats from 0 to 1.
  • To change the color of the grid, I modified the draw method. I replaced the color self.gridlines.set_color([(0.9,0.9,0.9,1)] * len(lines)) by something of my choosing.

就是这样,它只是有效.这不是最方便的方法,但是与编辑rc配置文件相比,它没有更多的工作.

And that's it, it just works. Not the most convenient, but it's not more work than editing a rc configuration file.

我没有重新创建 .pyc 文件.它不会重新创建自己,因为我没有以 root 身份运行我的 python 代码.我不介意 python 每次重新编译 .py 所需的额外毫秒数.

I did not recreate a .pyc file. It does not recreate itself because I do not run my python code as root. I don't mind the extra milliseconds that python needs to recompile the .py each time.

这篇关于如何更改matplotlib 3D图的轴的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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