簡體   English   中英

Python-Matplotlib-PyQT:繪制到QPixmap

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

我想可視化matplotlibs顏色圖(類似於http://matplotlib.org/examples/color/colormaps_reference.html ),並將其用作PyQt小部件中的QPixmaps。 想法是在matplotlib中創建圖,而無需實際顯示(或將其保存到文件中)並將其轉換為QPixmap。 這里提供的解決方案( Python-matplotlib-PyQT:將圖像復制到剪貼板 )似乎不起作用,可能是因為我不想顯示matplotlib圖。

我已經嘗試了以下方法,並且可行:

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