我想将使用matplotlib生成的绘图转换为rgb数组。就我而言,我想使用matplotlib绘制两个圆圈。目前,有两个问题:
以下是目前的代码:
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)