2013-03-12 86 views
1

我正在試圖在matplotlib圖上繪製一系列任意行。下面是我使用的代碼:matplotlib中的軸重疊行

import matplotlib.pyplot as pyplot 

def center_origin(axis): 
    '''Center the axis in the middle of the picture''' 
    axis.spines['right'].set_color('none') 
    axis.spines['top'].set_color('none') 
    axis.xaxis.set_ticks_position('bottom') 
    axis.spines['bottom'].set_position(('data',0)) 
    axis.yaxis.set_ticks_position('left') 
    axis.spines['left'].set_position(('data',0)) 

def render(lines): 
    figure = pyplot.figure(figsize=(4,4)) 
    axis = figure.add_subplot(1, 1, 1) 

    center_origin(axis)   

    for (x1, y1), (x2, y2) in lines: 
     axis.add_line(pyplot.Line2D((x1, x2), (y1, y2), color='red')) 

    axis.set_xlim(-1.2, 1.2) 
    axis.set_ylim(-1.2, 1.2) 
    return figure 

if __name__ == '__main__': 
    render([((1, 0), (0, 1)), 
      ((1, 0), (-1, 0)), 
      ((1, 0), (0, -1))]).show() 
    raw_input('block > ') 

它產生類似如下的圖表:

bad graph

目前,x軸掩蓋應該從運行紅線(1,0)到(-1,0)。我試圖將center_origin函數放在繪製線條之前和之後,但沒有任何變化。

我該如何讓matplotlib在軸上畫線?

+1

「zorder」屬性決定了線條在彼此之上繪製的順序。嘗試'pyplot.Line2D((x1,x2),(y1,y2),color ='red',zorder = 1)'。玩弄價值,你可能需要更高的東西。 – Robbert 2013-03-12 15:57:24

+1

@羅伯特 - 謝謝,那就是訣竅!如果你想把它寫成答案,我可以接受它/ upvote。 – Michael0x2a 2013-03-12 16:01:03

回答

4

屬性zorder確定線條彼此重疊的順序。嘗試pyplot.Line2D((x1, x2), (y1, y2), color='red', zorder = 1)。玩弄價值,你可能需要更高的東西。