2011-01-26 5 views

答えて

263

Matplotlibはこれをデフォルトで行います。

例:

import matplotlib.pyplot as plt 
import numpy as np 

x = np.arange(10) 

plt.plot(x, x) 
plt.plot(x, 2 * x) 
plt.plot(x, 3 * x) 
plt.plot(x, 4 * x) 
plt.show() 

Basic plot demonstrating color cycling

そして、あなたはすでに知っているかもしれませんが、あなたは簡単に伝説を追加することができます:あなたがコントロールしたい場合は

import matplotlib.pyplot as plt 
import numpy as np 

x = np.arange(10) 

plt.plot(x, x) 
plt.plot(x, 2 * x) 
plt.plot(x, 3 * x) 
plt.plot(x, 4 * x) 

plt.legend(['y = x', 'y = 2x', 'y = 3x', 'y = 4x'], loc='upper left') 

plt.show() 

Basic plot with legend

をサイクリングされる色:

import matplotlib.pyplot as plt 
import numpy as np 

x = np.arange(10) 

plt.gca().set_color_cycle(['red', 'green', 'blue', 'yellow']) 

plt.plot(x, x) 
plt.plot(x, 2 * x) 
plt.plot(x, 3 * x) 
plt.plot(x, 4 * x) 

plt.legend(['y = x', 'y = 2x', 'y = 3x', 'y = 4x'], loc='upper left') 

plt.show() 

Plot showing control over default color cycling

ビットを役に立てば幸い! matplotlibに慣れていない場合はthe tutorial is a good place to startです。

編集:

まず第一にあなたは多くを持っている場合、(> 5)使用すると、1つの図にプロットしたいもの、のいずれかの:

  1. 異なるプロット上に置く(使用を検討してくださいいくつかのサブプロット)、または
  2. それらを区別するために、色以外のもの(マーカーのスタイルや線の太さなど)を使用してください。

それ以外の場合は、と非常に一致します。 messy plot!あなたがやっていることを読んで、15種類の事柄をひとつの姿に詰め込もうとしない人には、すてきでください!

それを超えると、多くの人々が様々な程度で色盲をしており、微妙に異なる色を区別することは、理解できないほど多くの人々にとっては困難です。私が提供したいと思います

import matplotlib.pyplot as plt 
import numpy as np 

num_plots = 20 

# Have a look at the colormaps here and decide which one you'd like: 
# http://matplotlib.org/1.2.1/examples/pylab_examples/show_colormaps.html 
colormap = plt.cm.gist_ncar 
plt.gca().set_color_cycle([colormap(i) for i in np.linspace(0, 0.9, num_plots)]) 

# Plot several different functions... 
x = np.arange(10) 
labels = [] 
for i in range(1, num_plots + 1): 
    plt.plot(x, i * x + 5 * i) 
    labels.append(r'$y = %ix + %i$' % (i, 5*i)) 

# I'm basically just demonstrating several different legend options here... 
plt.legend(labels, ncol=4, loc='upper center', 
      bbox_to_anchor=[0.5, 1.1], 
      columnspacing=1.0, labelspacing=0.0, 
      handletextpad=0.0, handlelength=1.5, 
      fancybox=True, shadow=True) 

plt.show() 

Unique colors for 20 lines based on a given colormap

+2

注意、matplotlibの新しいバージョンでは、 'set_color_cycle'は廃止されていますので、' plt.gca().'を指定してください。set_prop_cycle(plt.cycler( 'color'、plt.cm.jet(np.linspace(0、1、num_plots))) ''あなたのニーズに合わせて 'plt.cm.YOUR_PREFERED_COLOR_MAP'を変更するだけです。 – Nate

+0

@JoeKington:説明は素晴らしく、ありがとうございます。 – Breeze

2

:あなたは本当に20比較的異なる色で一つの軸に20行を入れたい場合は、ここでそれを行うための一つの方法だ、と述べていたこと

前回の投稿で与えられた最後のループ回答のマイナーな改善(その投稿は正しいものであり、まだ受け入れられるはずです)。最後の例をラベル付けするときの暗黙の前提は、がX番目の時刻plotに対応する行でLISTにラベル番号Xを代入するということです。私はこれまでにこのアプローチに問題がありました。伝説を構築し、matplotlibsドキュメント(http://matplotlib.org/users/legend_guide.html#adjusting-the-order-of-legend-item)ごとに、そのラベルをカスタマイズするための推奨方法は、ラベルは、あなたが彼らがそうだと思う正確なプロットと一緒に行くことに温かい気持ちを持つことです。

... 
# Plot several different functions... 
labels = [] 
plotHandles = [] 
for i in range(1, num_plots + 1): 
    x, = plt.plot(some x vector, some y vector) #need the ',' per ** below 
    plotHandles.append(x) 
    labels.append(some label) 
plt.legend(plotHandles, labels, 'upper left',ncol=1) 

**:Matplotlib Legends not working

+3

ちょうどfyi: "label" kwargを使ってプロットし、引数なしで凡例を呼び出すこともできます。 –

14

あなたは、あなたが、私はこのソリューションを使用して、.linesを使用して、プロットから直接番号を取得し、それらをプロットしたらあなたは色を変更することができますプロットしようとしているプロットの数がわからない場合は、後で

それらの設定:

いくつかのランダムなデータ

import matplotlib.pyplot as plt 
import numpy as np 

fig1 = plt.figure() 
ax1 = fig1.add_subplot(111) 


for i in range(1,15): 
    ax1.plot(np.array([1,5])*i,label=i) 

あなたが必要とするコードの一部:

colormap = plt.cm.gist_ncar #nipy_spectral, Set1,Paired 
colors = [colormap(i) for i in np.linspace(0, 1,len(ax1.lines))] 
for i,j in enumerate(ax1.lines): 
    j.set_color(colors[i]) 


ax1.legend(loc=2) 

結果は以下の通りです:最後例えばenter image description here

関連する問題