Python - matplotlib - PyQT:绘制到 QPixmap [英] Python - matplotlib - PyQT: plot to QPixmap
问题描述
我想可视化matplotlibs颜色图(类似于 http://matplotlib.org/examples/color/colormaps_reference.html) 并将其用作 PyQt 小部件中的 QPixmap.这个想法是在 matplotlib 中创建绘图而不实际显示它(或将其保存到文件)并将其转换为 QPixmap.此处提供的解决方案(Python - matplotlib - PyQT:将图像复制到剪贴板) 似乎不起作用,可能是因为我不想显示 matplotlib 图.
I want to visualise matplotlibs colormaps (similar to http://matplotlib.org/examples/color/colormaps_reference.html) and use it as QPixmaps in PyQt widgets. The idea is to create the plots in matplotlib without actually showing it (or saving it to a file) and convert it to a QPixmap. The solution offered here (Python - matplotlib - PyQT: Copy image to clipboard) doesn't seem to work, maybe because I don't want to show the matplotlib plot.
推荐答案
我尝试了以下方法并且有效:
I have tried the following and that works:
def testColourMap(cmap):
sp = SubplotParams(left=0., bottom=0., right=1., top=1.)
fig = Figure((2.5,.2), subplotpars = sp)
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
ax.imshow(gradient, aspect=10, cmap=cmap)
ax.set_axis_off()
canvas.draw()
size = canvas.size()
width, height = size.width(), size.height()
im = QImage(canvas.buffer_rgba(), width, height, QImage.Format_ARGB32)
return QPixmap(im)
这篇关于Python - matplotlib - PyQT:绘制到 QPixmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!