简体   繁体   English

如何一次为轴的所有方面设置颜色?

[英]How can I set the color for all aspects of an axis at once?

I'd like to set the color of major and minor ticks, tick labels, and the axis itself all to the same color, but the only way I've come up with to accomplish this is something like 我想将主要和次要刻度线,刻度线标签和轴本身的颜色都设置为相同的颜色,但是我想出的唯一方法是像

fig, ax_a_mj = matplotlib.pyplot.subplots()

ax_a_mj.spines['bottom'].set_color('r')
ax_a_mj.tick_params(axis='x', colors='r')
ax_a_mj.tick_params(axis='x', colors='r', which='minor')
ax_a_mj.xaxis.label.set_color('r')

Is there no way to do this with a single statement? 仅凭一条语句就无法做到这一点吗?

How about the case where I want to do all axes, and I have four: 我想做所有轴的情况怎么样?我有四个:

fig, ax_a_mj = matplotlib.pyplot.subplots()
ax_a_me = ax_a_mj.twinx()
ax_p_mj = ax_a_mj.twiny()

ax_a_mj.spines['bottom'].set_color('r')
ax_a_mj.spines['left'].set_color('r')
ax_a_mj.spines['right'].set_color('r')
ax_a_mj.spines['top'].set_color('r')
ax_a_mj.tick_params(axis='x', colors='r', which='both')
ax_a_mj.tick_params(axis='y', colors='r', which='both')
ax_p_mj.tick_params(axis='x', colors='r', which='both')
ax_a_me.tick_params(axis='y', colors='r', which='both')
ax_a_mj.xaxis.label.set_color('r')
ax_a_mj.yaxis.label.set_color('r')
ax_p_mj.xaxis.label.set_color('r')
ax_a_me.yaxis.label.set_color('r')

If you are doing this a lot, just write your self a helper function 如果您经常这样做,只需编写自己的帮助函数即可

def colorize(ax_in, color_in):
    ax_in.spines['bottom'].set_color(color_in)
    ax_in.tick_params(axis='x', colors=color_in, which='both')
    ax_in.xaxis.label.set_color(color_in)

Note that which can be 'both' which will set both the major and minor ticks (doc) . 请注意, which可以是'both' ,将同时设置主要和次要刻度(doc)

If you are using this a lot , you can monkey patch it on to Axes 如果你正在使用这个有很多 ,你可以猴子修补它上Axes

matplotlib.axes.Axes.my_colorize = colorize

and then 接着

my_ax.my_colorize('r')

should work. 应该管用。

There is on going work to support style sheets by Tony Yu, the first bits of have already been merged into master ( https://github.com/matplotlib/matplotlib/pull/2236 ). Tony Yu正在进行支持样式表的工作,其中的头几部分已经合并到master( https://github.com/matplotlib/matplotlib/pull/2236 )中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM