繁体   English   中英

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

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

我想将主要和次要刻度线,刻度线标签和轴本身的颜色都设置为相同的颜色,但是我想出的唯一方法是像

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')

仅凭一条语句就无法做到这一点吗?

我想做所有轴的情况怎么样?我有四个:

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')

如果您经常这样做,只需编写自己的帮助函数即可

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)

请注意, which可以是'both' ,将同时设置主要和次要刻度(doc)

如果你正在使用这个有很多 ,你可以猴子修补它上Axes

matplotlib.axes.Axes.my_colorize = colorize

接着

my_ax.my_colorize('r')

应该管用。

Tony Yu正在进行支持样式表的工作,其中的头几部分已经合并到master( https://github.com/matplotlib/matplotlib/pull/2236 )中。

暂无
暂无

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

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