RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1430055
Accepted
Konstantin
Konstantin
Asked:2022-07-15 21:54:05 +0000 UTC2022-07-15 21:54:05 +0000 UTC 2022-07-15 21:54:05 +0000 UTC

绘制热图

  • 772

有一个大小为 500 x 200 的数据集(随机生成):

N = 500
M = 200
data = numpy.random.random((N, M))

为了以颜色表示数据,我使用了我的 save_heat_map 函数,它创建了一个相称的白色 QPixmap(500 x 200),然后调用 create_heat_map 函数,它从数据中确定最大值并根据来自的值逐像素着色像素数据集,即 每个像素对应于某个数据值(像素 (0,0) 的颜色将取决于 data[0,0])。然后保存结果的文件(大小为 500 x 200 的图片)。

问题:图片是500x200的时候很好,而不是5000x2000 =)如何让它更快?(颜色不重要)

import sys
from PySide2.QtGui import QPixmap, QColor
from PySide2.QtWidgets import QMainWindow, QApplication, QLabel, QHBoxLayout, QPushButton, QWidget
import numpy

# 1. Случайные числа в матрице N на M
N = 500
M = 200
data = numpy.random.random((N, M))


class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.title = "Image Viewer"
        self.setWindowTitle(self.title)
        central_widget = QWidget()
        box = QHBoxLayout()
        self.label = QLabel()
        self.button = QPushButton("Click me")
        self.button.clicked.connect(self.save_heat_map)
        box.addWidget(self.label)
        box.addWidget(self.button)
        central_widget.setLayout(box)
        self.setCentralWidget(central_widget)

    def save_heat_map(self):
        # создадим соразмерный pixmap_zone и сделаем его прозрачным
        pixmap_zone = QPixmap(N, M)
        pixmap_zone.fill(QColor(255, 255, 255, 255))
        # Нарисуем тепловую карту
        qimg_zone = pixmap_zone.toImage()
        heat_map = self.create_heat_map(data, N, M, qimg_zone)
        heat_map.save('test_output.png')


    def create_heat_map(self, arr, width: int, height: int, qimg_zone):
        max_el = arr.max()
        for x in range(width):
            for y in range(height):
                if arr[x, y] >= max_el:
                    qimg_zone.setPixelColor(x, y, QColor(255, 0, 0, 255))
                # красный
                elif max_el * 1.00 > arr[x, y] >= max_el * 0.90:
                    qimg_zone.setPixelColor(x, y, QColor(255, 10, 0, 255))
                # рыжий
                elif max_el * 0.90 > arr[x, y] >= max_el * 0.95:
                    qimg_zone.setPixelColor(x, y, QColor(255, 40, 0, 255))
                elif max_el * 0.95 > arr[x, y] >= max_el * 0.80:
                    qimg_zone.setPixelColor(x, y, QColor(255, 80, 0, 255))
                elif max_el * 0.80 > arr[x, y] >= max_el * 0.85:
                    qimg_zone.setPixelColor(x, y, QColor(255, 120, 0, 255))
                elif max_el * 0.85 > arr[x, y] >= max_el * 0.70:
                    qimg_zone.setPixelColor(x, y, QColor(255, 150, 0, 255))
                # желтый
                elif max_el * 0.70 > arr[x, y] >= max_el * 0.65:
                    qimg_zone.setPixelColor(x, y, QColor(255, 170, 0, 255))
                elif max_el * 0.65 > arr[x, y] >= max_el * 0.60:
                    qimg_zone.setPixelColor(x, y, QColor(255, 190, 0, 255))
                elif max_el * 0.60 > arr[x, y] >= max_el * 0.55:
                    qimg_zone.setPixelColor(x, y, QColor(255, 210, 0, 255))
                elif max_el * 0.55 > arr[x, y] >= max_el * 0.50:
                    qimg_zone.setPixelColor(x, y, QColor(255, 230, 0, 255))
                elif max_el * 0.50 > arr[x, y] >= max_el * 0.45:
                    qimg_zone.setPixelColor(x, y, QColor(255, 255, 0, 255))
                # салатовый
                elif max_el * 0.45 > arr[x, y] >= max_el * 0.475:
                    qimg_zone.setPixelColor(x, y, QColor(230, 255, 0, 255))
                elif max_el * 0.475 > arr[x, y] >= max_el * 0.450:
                    qimg_zone.setPixelColor(x, y, QColor(210, 255, 0, 255))
                elif max_el * 0.450 > arr[x, y] >= max_el * 0.425:
                    qimg_zone.setPixelColor(x, y, QColor(180, 255, 0, 255))
                elif max_el * 0.425 > arr[x, y] >= max_el * 0.400:
                    qimg_zone.setPixelColor(x, y, QColor(150, 255, 0, 255))
                # зеленый
                elif max_el * 0.400 > arr[x, y] >= max_el * 0.39:
                    qimg_zone.setPixelColor(x, y, QColor(100, 255, 0, 255))
                elif max_el * 0.39 > arr[x, y] >= max_el * 0.38:
                    qimg_zone.setPixelColor(x, y, QColor(70, 255, 0, 255))
                elif max_el * 0.38 > arr[x, y] >= max_el * 0.37:
                    qimg_zone.setPixelColor(x, y, QColor(40, 255, 0, 255))
                elif max_el * 0.37 > arr[x, y] >= max_el * 0.36:
                    qimg_zone.setPixelColor(x, y, QColor(10, 255, 0, 255))
                elif max_el * 0.36 > arr[x, y] >= max_el * 0.35:
                    qimg_zone.setPixelColor(x, y, QColor(0, 255, 0, 255))

                # голубой
                elif max_el * 0.35 > arr[x, y] >= max_el * 0.245:
                    qimg_zone.setPixelColor(x, y, QColor(0, 255, 40, 255))
                elif max_el * 0.245 > arr[x, y] >= max_el * 0.240:
                    qimg_zone.setPixelColor(x, y, QColor(0, 255, 80, 255))
                elif max_el * 0.240 > arr[x, y] >= max_el * 0.235:
                    qimg_zone.setPixelColor(x, y, QColor(0, 255, 100, 255))
                elif max_el * 0.235 > arr[x, y] >= max_el * 0.23:
                    qimg_zone.setPixelColor(x, y, QColor(0, 255, 120, 255))
                elif max_el * 0.23 > arr[x, y] >= max_el * 0.225:
                    qimg_zone.setPixelColor(x, y, QColor(0, 255, 150, 255))
                elif max_el * 0.225 > arr[x, y] >= max_el * 0.22:
                    qimg_zone.setPixelColor(x, y, QColor(0, 255, 210, 255))
                elif max_el * 0.22 > arr[x, y] >= max_el * 0.215:
                    qimg_zone.setPixelColor(x, y, QColor(0, 255, 240, 255))
                elif max_el * 0.215 > arr[x, y] >= max_el * 0.21:
                    qimg_zone.setPixelColor(x, y, QColor(0, 255, 255, 255))

                # темно-голубой
                elif max_el * 0.21 > arr[x, y] >= max_el * 0.19:
                    qimg_zone.setPixelColor(x, y, QColor(0, 220, 255, 255))
                elif max_el * 0.19 > arr[x, y] >= max_el * 0.18:
                    qimg_zone.setPixelColor(x, y, QColor(0, 210, 255, 255))
                elif max_el * 0.18 > arr[x, y] >= max_el * 0.17:
                    qimg_zone.setPixelColor(x, y, QColor(0, 200, 255, 255))
                elif max_el * 0.17 > arr[x, y] >= max_el * 0.16:
                    qimg_zone.setPixelColor(x, y, QColor(0, 190, 255, 255))
                elif max_el * 0.16 > arr[x, y] >= max_el * 0.15:
                    qimg_zone.setPixelColor(x, y, QColor(0, 160, 255, 255))
                elif max_el * 0.15 > arr[x, y] >= max_el * 0.14:
                    qimg_zone.setPixelColor(x, y, QColor(0, 150, 255, 255))
                #     синий
                elif max_el * 0.14 > arr[x, y] >= max_el * 0.13:
                    qimg_zone.setPixelColor(x, y, QColor(0, 130, 255, 255))
                elif max_el * 0.13 > arr[x, y] >= max_el * 0.125:
                    qimg_zone.setPixelColor(x, y, QColor(0, 110, 255, 255))
                elif max_el * 0.125 > arr[x, y] >= max_el * 0.115:
                    qimg_zone.setPixelColor(x, y, QColor(0, 90, 255, 255))
                elif max_el * 0.115 > arr[x, y] >= max_el * 0.11:
                    qimg_zone.setPixelColor(x, y, QColor(0, 70, 255, 255))
                elif max_el * 0.11 > arr[x, y] >= max_el * 0.108:
                    qimg_zone.setPixelColor(x, y, QColor(0, 50, 255, 255))
                elif max_el * 0.108 > arr[x, y] >= max_el * 0.107:
                    qimg_zone.setPixelColor(x, y, QColor(0, 40, 255, 255))
                elif max_el * 0.107 > arr[x, y] >= max_el * 0.106:
                    qimg_zone.setPixelColor(x, y, QColor(0, 30, 255, 255))
                elif max_el * 0.106 > arr[x, y] >= max_el * 0.102:
                    qimg_zone.setPixelColor(x, y, QColor(0, 20, 255, 255))
                elif max_el * 0.102 > arr[x, y] >= max_el * 0.101:
                    qimg_zone.setPixelColor(x, y, QColor(0, 0, 255, 255))
        return qimg_zone


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
pyqt5 pyside2
  • 1 1 个回答
  • 23 Views

1 个回答

  • Voted
  1. Best Answer
    Konstantin
    2022-07-17T05:11:32Z2022-07-17T05:11:32Z

    解决方案不是我的:

    import numpy as np
    from PIL import Image
    N = 500
    M = 200
    data = numpy.random.random((N, M))
    bins = np.array([0.00, 0.10, 0.15, 0.25, 0.30, 0.40, 0.6, 0.8, 0.95])
    palette=np.array([[0, 30, 255, 255], [0, 80, 255, 255],[0, 100, 255, 255], 
                      [0, 255, 255, 255], [20, 255, 255, 255],[100, 100, 255, 255], 
                      [200, 50, 255, 255], [255, 255, 100, 255],[255, 255, 0, 255]]) 
    digitize=np.digitize(data, bins, right=True)
    digitize = np.expand_dims(digitize, axis=2)
    im=np.choose(digitize, palette, mode='clip' )
    im=np.uint8(im)
    PIL_image = Image.fromarray(np.uint8(im)).convert('RGBA')
    PIL_image.save('out_.png')
    
    • 0

相关问题

  • PyQt5:带延迟的线路输出

  • 如何制作工具提示

  • 在 PyQt5 中读取字符串

  • 如何更改 QLabel 的背景?

  • python,PyQt5 库中的错误:ImportError: cannot import name 'QtWebKitWidgets'

  • PyQt5,选择图像时出现“无法读取内存”错误

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5