簡體   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