2016-03-18 55 views
4

我一直在這個問題附近玩弄,並接近我想要的,但錯過了額外的一兩行。蟒蛇matplotlib用線條顏色漸變和colorbar

基本上,我想繪製一條線,其顏色變化給定了第三個數組的值。潛伏在我發現這個效果很好(儘管很慢),並表示問題

import numpy as np 
import matplotlib.pyplot as plt 
c = np.arange(1,100) 
x = np.arange(1,100) 
y = np.arange(1,100) 

cm = plt.get_cmap('hsv') 

fig = plt.figure(figsize=(5,5)) 
ax1 = plt.subplot(111) 

no_points = len(c) 
ax1.set_color_cycle([cm(1.*i/(no_points-1)) 
        for i in range(no_points-1)]) 

for i in range(no_points-1): 
    bar = ax1.plot(x[i:i+2],y[i:i+2]) 
plt.show() 

,給了我這樣的:

Single line with a color change

我希望能夠沿着包括彩條與此情節。到目前爲止,我還沒有能夠破解它。可能會有其他行包含在不同的x,y中,但是它們是相同的c,所以我認爲Normalize對象是正確的路徑。

更大的圖片是這個圖是2x2子圖網格的一部分。我已經使用matplotlib.colorbar.make_axes(ax4)爲其中的ax4和第4個子圖創建了顏色條軸對象的空間。

+1

查看第二個例子:http://matplotlib.org/examples/pylab_examples/multicolored_line.html你應該使用'LineCollection'這是'ScalarMappable'子類,所以你可以將藝術家傳遞給'fig.colorbar ()'得到一個顏色條。 – tacaswell

回答

7

看看在Matplotlib gallerydpsanders' colorline notebookmulticolored_line example

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.collections as mcoll 

def multicolored_lines(): 
    """ 
    http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb 
    http://matplotlib.org/examples/pylab_examples/multicolored_line.html 
    """ 

    x = np.linspace(0, 4. * np.pi, 100) 
    y = np.sin(x) 
    fig, ax = plt.subplots() 
    lc = colorline(x, y, cmap='hsv') 
    plt.colorbar(lc) 
    plt.xlim(x.min(), x.max()) 
    plt.ylim(-1.0, 1.0) 
    plt.show() 

def colorline(
     x, y, z=None, cmap='copper', norm=plt.Normalize(0.0, 1.0), 
     linewidth=3, alpha=1.0): 
    """ 
    http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb 
    http://matplotlib.org/examples/pylab_examples/multicolored_line.html 
    Plot a colored line with coordinates x and y 
    Optionally specify colors in the array z 
    Optionally specify a colormap, a norm function and a line width 
    """ 

    # Default colors equally spaced on [0,1]: 
    if z is None: 
     z = np.linspace(0.0, 1.0, len(x)) 

    # Special case if a single number: 
    # to check for numerical input -- this is a hack 
    if not hasattr(z, "__iter__"): 
     z = np.array([z]) 

    z = np.asarray(z) 

    segments = make_segments(x, y) 
    lc = mcoll.LineCollection(segments, array=z, cmap=cmap, norm=norm, 
           linewidth=linewidth, alpha=alpha) 

    ax = plt.gca() 
    ax.add_collection(lc) 

    return lc 

def make_segments(x, y): 
    """ 
    Create list of line segments from x and y coordinates, in the correct format 
    for LineCollection: an array of the form numlines x (points per line) x 2 (x 
    and y) array 
    """ 

    points = np.array([x, y]).T.reshape(-1, 1, 2) 
    segments = np.concatenate([points[:-1], points[1:]], axis=1) 
    return segments 

multicolored_lines() 

enter image description here


請注意,調用plt.plot輕車熟路往往殺性能。 使用LineCollection構建多色線段要快得多。

+0

我是關於粘貼一個答案,而不是我只會upvote :-p – tacaswell

+0

太棒了!謝謝你,正是我在找的! – TJG

+0

unutbu像往常一樣優秀的答案。有人會認爲這應該更容易處理一些默認的matplotlib參數。 – Gabriel