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 > ')
运行后生成的图表看起来是这样的:
目前,x轴遮住了应该从(1, 0)到(-1, 0)的红线。我尝试在绘制线条之前和之后放置center_origin
函数,但没有任何变化。
我该如何让matplotlib在坐标轴上方绘制我的线条呢?
1 个回答
5
属性 zorder
决定了线条绘制的顺序,也就是说,哪些线条会在其他线条的上面。你可以试试 pyplot.Line2D((x1, x2), (y1, y2), color='red', zorder = 1)
这个代码。可以尝试改变这个值,可能需要设置得更高一些。