简体   繁体   English

Python-Matplotlib-PyQT:绘制到QPixmap

[英]Python - matplotlib - PyQT: plot to QPixmap

I want to visualise matplotlibs colormaps (similar to http://matplotlib.org/examples/color/colormaps_reference.html ) and use it as QPixmaps in PyQt widgets. 我想可视化matplotlibs颜色图(类似于http://matplotlib.org/examples/color/colormaps_reference.html ),并将其用作PyQt小部件中的QPixmaps。 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. 想法是在matplotlib中创建图,而无需实际显示(或将其保存到文件中)并将其转换为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. 这里提供的解决方案( Python-matplotlib-PyQT:将图像复制到剪贴板 )似乎不起作用,可能是因为我不想显示matplotlib图。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM