在Matplotlib中如何绘制图表但不显示它。

huangapple go评论83阅读模式
英文:

How to draw a plot but not showing it in matplotlib

问题

I wish to utilize the easy plotting feature of Matplotlib to generate some bitmaps as templates or (rather large) convolution kernels.

我希望利用Matplotlib的简便绘图功能来生成一些位图作为模板或(相当大的)卷积核。

I followed this post to convert the plot into a Numpy array:

我按照此帖子的方法将绘图转换为Numpy数组:

def DrawKernel(x = 0.5, y = 0.5, radius = 0.49, dimension = 256):
    '''Make a solid circle and return a numpy array of it'''
    
    DPI = 100
    figure, axes = plt.subplots(figsize=(dimension/DPI, dimension/DPI), dpi=DPI)
    canvas = FigureCanvas(figure)

    Drawing_colored_circle = plt.Circle(( x, y ), radius, color='k')
    
    axes.set_aspect( 1 )
    axes.axis("off")
    axes.add_artist( Drawing_colored_circle )

    canvas.draw()  

    # Convert figure into a numpy array
    img = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
    img = img.reshape(dimension, dimension, 3)

    return img

But this seems to show the plot every time it's called (screenshot in Google Colab) :

但似乎每次调用此函数都会显示绘图(在Google Colab中截图):

在Matplotlib中如何绘制图表但不显示它。

Since this function will be invoked frequently later, I don't want to see every plot generated with it. Is there a way to let it be generated but not displayed?

由于此函数将在以后频繁调用,我不想看到每次生成的绘图。是否有一种方法可以生成它但不显示?

英文:

I wish to utilize the easy plotting feature of Matplotlib to generate some bitmaps as templates or (rather large) convolution kernels.

I followed this post to convert the plot into a Numpy array:

def DrawKernel(x = 0.5, y = 0.5, radius = 0.49, dimension = 256):
    '''Make a solid circle and return a numpy array of it'''

    DPI = 100
    figure, axes = plt.subplots(figsize=(dimension/DPI, dimension/DPI), dpi=DPI)
    canvas = FigureCanvas(figure)

    Drawing_colored_circle = plt.Circle(( x, y ), radius, color='k')
    
    axes.set_aspect( 1 )
    axes.axis("off")
    axes.add_artist( Drawing_colored_circle )

    canvas.draw()  

    # Convert figure into an numpy array
    img = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
    img = img.reshape(dimension, dimension, 3)

    return img

But this seems to show the plot every time it's called (screenshot in Google Colab) :

在Matplotlib中如何绘制图表但不显示它。

Since this function will be invoked frequently later, I don't want to see every plot generated with it. Is there a way to let it be generated but not displayed?

答案1

得分: 1

Here is the translated code portion:

只需在`plt.subplots`中添加`visible=False`:

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
def DrawKernel(x = 0.5, y = 0.5, radius = 0.49, dimension = 256):
    '''创建一个实心圆并返回其numpy数组'''
    
    DPI = 100
    figure, axes = plt.subplots(figsize=(dimension/DPI, dimension/DPI), dpi=DPI, visible=False)
    canvas = FigureCanvas(figure)

    
    Drawing_colored_circle = plt.Circle(( x, y ), radius, color='k')
    
    axes.set_aspect( 1 )
    axes.axis("off")
    axes.add_artist( Drawing_colored_circle )
    
    canvas.draw()  
    
    # 将图形转换为numpy数组
    img = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
    img = img.reshape(dimension, dimension, 3)
    
    return img
DrawKernel();

Please note that I have translated the code part only, as per your request.

英文:

Just pass visible=False in plt.subplots:

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
def DrawKernel(x = 0.5, y = 0.5, radius = 0.49, dimension = 256):
    '''Make a solid circle and return a numpy array of it'''

    DPI = 100
    figure, axes = plt.subplots(figsize=(dimension/DPI, dimension/DPI), dpi=DPI, visible=False)
    canvas = FigureCanvas(figure)


    Drawing_colored_circle = plt.Circle(( x, y ), radius, color='k')
    
    axes.set_aspect( 1 )
    axes.axis("off")
    axes.add_artist( Drawing_colored_circle )

    canvas.draw()  

    # Convert figure into an numpy array
    img = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
    img = img.reshape(dimension, dimension, 3)

    return img
DrawKernel();

huangapple
  • 本文由 发表于 2023年5月10日 23:25:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定