2011-01-21 33 views
48

matplotlibとPyQtを使用して作成したプロットの軸の色やティック、値ラベルを変更したいと思います。matplotlibのプロットの軸、ティック、ラベルの色を変更する

アイデア? (潜在的に重複した質問よりもわずかにきれいメソッドを使用して)簡単な例として、

+0

を軸用の複製します。http: //stackoverflow.com/questions/1982770/matplotlib-changing-the-color-of-anaxis – Mark

答えて

73

import matplotlib.pyplot as plt 

fig = plt.figure() 
ax = fig.add_subplot(111) 

ax.plot(range(10)) 
ax.set_xlabel('X-axis') 
ax.set_ylabel('Y-axis') 

ax.spines['bottom'].set_color('red') 
ax.spines['top'].set_color('red') 
ax.xaxis.label.set_color('red') 
ax.tick_params(axis='x', colors='red') 

plt.show() 

alt text

+0

これまでにありがとうございました。 ax35_params(axis = 'x'、colors = 'red') AxesSubplotには属性 'tick_params'がありません。なぜなのかご存知ですか? –

+11

ええええええええええええええええと、あなたが使用しているmatplotlibのバージョンは何ですか?この問題を回避するには、ax.xaxis.get_ticklabels()] 'のtに対して、[ax.xaxis.get_ticklines()]'および '[t.set_color( 'red')のtに対して' [t.set_color( 'red' 。 –

11

あなたが変更するいくつかの図またはサブプロットを持っている場合は、それが役に立つことができそれぞれを個別に変更するのではなく、matplotlib context managerを使用して色を変更します。コンテキストマネージャを使用すると、直後のインデントされたコードに対してのみrcパラメータを一時的に変更できますが、グローバルrcパラメータには影響しません。

このスニペットでは、軸、ティック、ティックラベルの色が変更された最初のものとデフォルトのrcパラメータを持つ2つ目の図が表示されます。

import matplotlib.pyplot as plt 
with plt.rc_context({'axes.edgecolor':'orange', 'xtick.color':'red', 'ytick.color':'green', 'figure.facecolor':'white'}): 
    # Temporary rc parameters in effect 
    fig, (ax1, ax2) = plt.subplots(1,2) 
    ax1.plot(range(10)) 
    ax2.plot(range(10)) 
# Back to default rc parameters 
fig, ax = plt.subplots() 
ax.plot(range(10)) 

enter image description here

enter image description here

あなたは、キーワードを検索するために使用可能なすべてのrcのパラメータを表示し、リストの内包を使用するplt.rcParamsを入力することができます。

# Search for all parameters containing the word 'color' 
[(param, value) for param, value in plt.rcParams.items() if 'color' in param] 
関連する問題