【发布时间】:2013-03-12 15:24:27
【问题描述】:
我目前正在尝试在 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 在轴上画线?
【问题讨论】:
-
属性
zorder决定了线条在彼此之上绘制的顺序。试试pyplot.Line2D((x1, x2), (y1, y2), color='red', zorder = 1)。玩转价值,你可能需要更高的东西。 -
@Robbert -- 谢谢,成功了!如果你想把它写下来作为答案,我可以接受/点赞。
标签: python matplotlib graphing