MatPlotLib:覆盖圆是透明的,而不是不透明的

时间:2018-11-24 18:10:33

标签: matplotlib

我使用MatPlotLib库生成SVG文件。

我试图得到一个黑边白脸的圆圈;脸必须透明。我使用了参数edgecolor,facecolor,alpha,fill,但是没有组合可以满足我的需求。轴和直线被圆覆盖:

import os
import locale
import matplotlib.pyplot as plt
import matplotlib.lines as mlines

files = []

# -----------------------------------------------------------------------------
class Fig:
    def __init__(self, filename, figsize = [4,4]):
        self.filename = filename 
        self.fig = plt.figure(figsize=figsize)
        self.fig.gca().axis('off')

    def save(self):
        global files
        files.append(self.filename)
        self.fig.savefig(self.filename+'.svg')


# -----------------------------------------------------------------------------
class FigNotHomeo(Fig):
    def __init__(self, filename, figsize = [4, 4]):
        Fig.__init__(self, filename, figsize)
        ax = self.fig.gca()
        ax.set_xlim(-1.1,1.1)
        ax.set_ylim(-1.05, 1.05)
        ax.axis('on')
        ax.spines['left'].set_position('center')
        ax.spines['bottom'].set_position('center')
        ax.spines['right'].set_color('none')
        ax.spines['top'].set_color('none')
        ax.xaxis.set_ticks([-1,1])
        ax.yaxis.set_ticks([-1,1])
        ax.tick_params( axis='y', labeltop= True ) # does not work
        x = [1,-1,1,0]
        y = [1,-1,-1,0]
        ax.add_line(mlines.Line2D(x,y, color='black'))
        # facecolor='white', edgecolor = 'black', fill = True, alpha=1, clip_on=True
        c = plt.Circle((0, 0), radius=0.03, facecolor='white', edgecolor = 'black',  alpha=1, fill = True, clip_on=False)
        ax.add_patch(c)

# -----------------------------------------------------------------------------
def main():
    FigNotHomeo('NotHomeo', figsize=[6, 6]).save()

if __name__== "__main__":
    main()

1 个答案:

答案 0 :(得分:2)

面片的zorder通常比线条低。

import matplotlib.pyplot as plt

circle = plt.Circle((.5, .5), radius=0.2)
plt.gca().add_patch(circle)

line = plt.Line2D([0,1],[0,1], color='black')
plt.gca().add_line(line)

plt.show()

enter image description here

但是,您当然可以根据需要设置zorder。

import matplotlib.pyplot as plt

circle = plt.Circle((.5, .5), radius=0.2, zorder=3)
plt.gca().add_patch(circle)

line = plt.Line2D([0,1],[0,1], color='black', zorder=2)
plt.gca().add_line(line)

plt.show()

enter image description here