matplotlib绘制直线和数字

2024-09-25 00:22:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在conv2D中绘制特征。绘图工作正常,但matplotlib在我的绘图中绘制线条。为什么

Image to the lines

我使用函数fig2img和fig2data将plt转换为图像,并使用imshow绘制此图像

    def plot_Filters(self, feature_maps, num1, num2):
            ix     = 1
            ax = self.figure.add_subplot(111)
            fig,ax1 = plt.subplots(111)
            for _ in range(num2):
                for _ in range(num1):
                    ax1 = plt.subplot(num2, num1, ix)
                    ax1.set_axis_off()
                    #plt.title(ix)
                    plt.axis('off')
                    plt.imshow(feature_maps[0, :, :, ix-1], cmap='gray')
                    ix += 1

            ax.imshow(self.fig2img(fig))


 def fig2data ( self, fig ):
        """
        @brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels and return it
        @param fig a matplotlib figure
        @return a numpy 3D array of RGBA values
        """
        # draw the renderer
        fig.canvas.draw ( )

        # Get the RGBA buffer from the figure
        w,h = fig.canvas.get_width_height()
        buf = np.fromstring ( fig.canvas.tostring_rgb(), dtype=np.uint8 )
        buf.shape = ( w, h, 3 )

        # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
        buf = np.roll ( buf, 1, axis = 2 )
        return buf

    def fig2img ( self, fig ):
        """
        @brief Convert a Matplotlib figure to a PIL Image in RGBA format and return it
        @param fig a matplotlib figure
        @return a Python Imaging Library ( PIL ) image
        """
        # put the figure pixmap into a numpy array
        buf = self.fig2data ( fig )
        w, h, d = buf.shape
        return IMM.frombytes( "RGB", ( w ,h ), buf.tostring( ) )

Tags: theinselfreturnmatplotlibfig绘制plt