简体   繁体   English

pyqtgraph 中的 matplotlib.pyplot.clim 对应

[英]matplotlib.pyplot.clim counterpart in pyqtgraph

I want to apply a colormap ("hot") only to a certain range of values of an image.我只想将颜色图(“热”)应用于图像的特定范围值。 I already know this functionality from matplotlib.pyplot.clim where you have to specify a minimum and a maximum value for the scaling.我已经从 matplotlib.pyplot.clim 了解此功能,您必须在其中指定缩放的最小值和最大值。 As I understood it, you should use setLevels([min,max]) for this.据我了解,您应该为此使用 setLevels([min,max]) 。 I have included a minimal example which shows that both representations are unfortunately not equivalent.我已经包含了一个最小的例子,它表明不幸的是这两种表示并不等同。

import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import os
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui#, QtWidgets
#import PyQt5.QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, QFileDialog, \
    QScrollArea, QVBoxLayout, QGroupBox, QLabel, QFormLayout, QComboBox, QHBoxLayout
from skimage import io    
import sys


def main():
    imarray = np.random.rand(100, 100) * 515
    imarray = np.clip(imarray, 0, 515)
    plt_example(imarray)
    pyqtgraph_example(imarray)

def plt_example(data):
    plt.imshow(data, cmap='hot')
    c = plt.colorbar()
    plt.clim(0, 200)
    plt.show()


def pyqtgraph_example(data):
    colormap = cm.get_cmap("hot")
    colormap._init()
    lut = (colormap._lut * 255).view(np.ndarray)
    app = pg.mkQApp("Test")

    
    win = pg.GraphicsLayoutWidget()
    win.show()
    win.setWindowTitle('pyqtgraph example: ImageItem')
    view = win.addViewBox()

    ## lock the aspect ratio so pixels are always square
    view.setAspectLocked(True)

    ## Create image item
    img = pg.ImageItem()
    view.addItem(img)

    img.setImage(data)
    img.setLevels([0, 200]) # same functionality as clim?
    img.setLookupTable(lut)

    app.exec()

if __name__ == "__main__":
    main()

Do you know a way how I can get the same representation in pyqtgraph as with clim in matplotlib?你知道我如何在 pyqtgraph 中获得与 matplotlib 中的 clim 相同的表示吗? The first image shows the result using matplotlib and the second shows the result using pyqtgraph.第一张图显示使用 matplotlib 的结果,第二张图显示使用 pyqtgraph 的结果。 在此处输入图像描述 在此处输入图像描述

This question made its way to the pyqtgraph issue tracker here: https://github.com/pyqtgraph/pyqtgraph/pull/1985#issuecomment-932223288这个问题进入了 pyqtgraph 问题跟踪器: https://github.com/pyqtgraph/pyqtgraph/pull/1985#issuecomment-932223288

Here is some code to generate identical results between the two libraries这是一些在两个库之间生成相同结果的代码

import matplotlib.pyplot as plt
import numpy as np
import pyqtgraph as pg

def main():
    imarray = np.random.rand(100, 100) * 515
    imarray = np.clip(imarray, 0, 515)
    plt_example(imarray)
    pyqtgraph_example(imarray)

def plt_example(data):
    plt.imshow(data, cmap='hot')
    c = plt.colorbar()
    plt.clim(0, 200)
    plt.show(block=False)

def pyqtgraph_example(data):
    app = pg.mkQApp("Test")
    win = pg.GraphicsLayoutWidget()
    win.setWindowTitle('pyqtgraph example: ImageItem')
    win.show()

    # add plot with correct axis orientation and locked aspect ratio
    plot = win.addPlot()
    plot.setAspectLocked(True)
    plot.invertY()

    # Create image item
    img = pg.ImageItem(data, axisOrder='row-major')
    plot.addItem(img)

    # Create color bar and have it control image levels    
    cmap = pg.colormap.getFromMatplotlib('hot')
    cbi = pg.ColorBarItem(colorMap=cmap)
    cbi.setImageItem(img, insert_in=plot)
    cbi.setLevels([0, 200]) # colormap range

    app.exec()

if __name__ == "__main__":
    main()

The poster of this code in the issue tracker noted that the "overflow" values seem to render to white in matplotlib, and black to pyqtgraph.问题跟踪器中此代码的发布者指出,“溢出”值似乎在 matplotlib 中呈现为白色,而在 pyqtgraph 中呈现为黑色。 In addition, the pyqtgraph image is rotated 90 degrees left since the axis order is not set and the y-axis is likely reversed.此外,pyqtgraph 图像向左旋转 90 度,因为未设置轴顺序并且 y 轴可能反转。

to get the right rotation orientation, the code has two lines of note为了获得正确的旋转方向,代码有两行注释

plot.invertY()

and

pg.ImageItem(data, axisOrder='row-major')

Hopefully this helps!希望这有帮助!

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

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