英文:
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中截图):
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) :
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论