简体   繁体   中英

Axis overlapping line in matplotlib

I'm currently trying to draw a series of arbitrary lines on a matplotlib graph. Here is the code I'm using:

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 > ')

It produces a graph that looks like this:

坏图

Currently, the x-axis is covering up the red line that should run from (1, 0) to (-1, 0). I tried placing the center_origin function to both before and after I drew the lines, but nothing changed.

How can I make matplotlib draw my lines over the axis?

The attribute zorder determines the order in which the lines are drawn on top of each other. Try pyplot.Line2D((x1, x2), (y1, y2), color='red', zorder = 1) . Play around with the value, you may need something higher.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM