Why is matplotlib plotting my circles as ovals?

前端 未结 3 1789
-上瘾入骨i
-上瘾入骨i 2020-12-13 07:20

Is there a way to get matplotlib to plot a perfect circle? They look more like ovals.

3条回答
  •  旧时难觅i
    2020-12-13 07:38

    I've encounter the same problem today and I think I might have a more flexible solution. Two main problems remain with the previous answer (if you don't use the equal aspect function). First if you resize your whole graph, the proportion will not be the same since the number of pixels will change. Second point, this trick do not work if you don't have the same lim for the xaxis and the yaxis.

    This solution tricks mpl using a custom object. Indeed, whenever you change one of your axis lim or your graph size, mpl will call an internal function which will take the width and height value of the ellipse multiplied by the transform function value. Since the width and height value is stored in the ellipse object, one way is to create a custom object with a value updated whenever the function is called, based on the current ax properties :

    import matplotlib.pyplot as plt
    from matplotlib.patches import Ellipse
    
    class GraphDist() :
        def __init__(self, size, ax, x=True) :
            self.size = size
            self.ax = ax
            self.x = x
    
        @property
        def dist_real(self) :
            x0, y0 = self.ax.transAxes.transform((0, 0)) # lower left in pixels
            x1, y1 = self.ax.transAxes.transform((1, 1)) # upper right in pixes
            value = x1 - x0 if self.x else y1 - y0
            return value
    
        @property
        def dist_abs(self) :
            bounds = self.ax.get_xlim() if self.x else self.ax.get_ylim()
            return bounds[0] - bounds[1]
    
        @property
        def value(self) :
            return (self.size / self.dist_real) * self.dist_abs
    
        def __mul__(self, obj) :
            return self.value * obj
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_xlim((0,10))
    ax.set_ylim((0,5))
    width = GraphDist(10, ax, True)
    height = GraphDist(10, ax, False)
    ax.add_artist(Ellipse((1, 3), width, height))
    plt.show()
    

提交回复
热议问题