PeptideChain
PeptideChain

Reputation: 563

MatPlotLib : covering circle is transparent instead of opaque

I generate an SVG file with the library MatPlotLib.

I'm trying to get a circle with black edge and white face; the face has to be not transparent. I played with the parameters edgecolor, facecolor, alpha, fill but no combination gives me what I want. Axis and lines get not covered by the circle:

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

Upvotes: 0

Views: 523

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339310

Patches generally have a lower zorder than lines.

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

However, you may of course set the zorder to your desire.

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

Upvotes: 2

Related Questions