将绘图转换为RGB数组

时间:2017-11-16 12:59:57

标签: python matplotlib rgb

我想将使用matplotlib生成的绘图转换为rgb数组。就我而言,我想使用matplotlib绘制两个圆圈。目前,有两个问题:

  1. 您仍然可以看到轴占用的空间
  2. 圈子在结果rgb数组中不再是圆圈
  3. 以下是目前的代码:

    import matplotlib.pyplot as plt    
    from matplotlib.patches import Circle
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    import numpy as np
    

    生成情节:

    def drawImage(color, posx, posy, radius):
    
        posx_left, posx_right = posx
        posy_left, posy_right = posy
    
        radius_left, radius_right = radius
    
        fig = Figure()
        canvas = FigureCanvas(fig)
        ax = fig.gca()
    
        ax.axis('off')
        ax.set_axis_off()
    
        ax = fig.add_subplot(1, 1, 1)
    
        circle_left = plt.Circle((posx_left, posy_left), radius=radius_left,color=color)
        ax.add_patch(circle_left)
    
        circle_right = plt.Circle((posx_right, posy_right), radius=radius_right,color=color)
        ax.add_patch(circle_right)
    
        fig.tight_layout(pad=0)
        fig.canvas.draw()
        width, height = fig.get_size_inches() * fig.get_dpi()
        img = np.fromstring(fig.canvas.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3)
        return img
    

    生成绘图并保存为数组:

    color = "blue"
    radius = 0.2, 0.12
    posx = 0.0,0.8
    posy = 0.3,0.7
    img = drawImage(color,posx,posy,radius)
    plt.imshow(img)
    

    Final plot

0 个答案:

没有答案