0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

如何用python对生成的map图进行上色呢?

sanyue7758 来源:Tom聊芯片智造 2023-09-26 09:32 次阅读

前几期,有朋友让我用python将cp的测试数据转化为map

但是,他又想把特定的测量数据转化为map图后,进行上色,即不同的测试数据能够呈现不同的颜色,以便于直观的观察其趋势。

数据样式:

bc7a372c-5bba-11ee-939d-92fbcf53809c.png

左边列是序号,中间列是XY,X-0016Y0079表示的是(X,Y)坐标为(16,79),最右行是测试数据。序号最大值为13278,即这个wafer有13278粒完成测试,得到了cp的测试数据。

我的思路:

1,将现有数据按照坐标,转化为map图,并将测试数值一一对应填入map图中。

2,有时测试标准是不一样的,所以可以手动设置标准值,这个在运行程序时能够弹出对话框,让使用者能够随意更改标准。低于标准值的测量值单元格呈现浅红色到红色的渐变,高于标准值的测量值呈现浅绿到紫色的渐变。

做出的效果:

1,双击“map上色.exe”运行程序

2,选择要上色的测试数据文件

bc7f807e-5bba-11ee-939d-92fbcf53809c.png

3,手动设置标准值

bc98c9da-5bba-11ee-939d-92fbcf53809c.png

4,设置文件名

bca11874-5bba-11ee-939d-92fbcf53809c.png

5,在原文件地址下输出包含map图的文件

bcb65284-5bba-11ee-939d-92fbcf53809c.png

6,打开文件

bcbc40ea-5bba-11ee-939d-92fbcf53809c.png

这样就很直观地看出测量值的分布图来了。

原代码如下,欢迎参考:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.styles import PatternFill
from tkinter.simpledialog import askfloat
from tkinter import Tk
from tkinter import filedialog
import colorsys
from tkinter.simpledialog import askstring
def get_threshold(threshold_type):
    print(f"Getting threshold for {threshold_type}...")
    root = Tk()
    root.withdraw()

    if threshold_type == "IL":
        threshold = askfloat(f"Input {threshold_type} Threshold", f"Enter the {threshold_type} threshold:")
        return threshold
    elif threshold_type == "FC":
        threshold_range_str = askstring(f"Input {threshold_type} Threshold Range",
                                        f"Enter the {threshold_type} threshold range (e.g., 'min,max'):")
        print(f"User input for FC threshold: {threshold_range_str}")
        try:
            min_threshold, max_threshold = map(float, threshold_range_str.split(','))
            return min_threshold, max_threshold
        except ValueError:
            print("Invalid input. Please enter two numbers separated by a comma.")
            return None
    root.destroy()
def color_map(value, threshold, data_min, data_max):
    # 正常化值到 [threshold, data_max] 区间,从极淡绿(144,238,144)到紫(128,0,128)
    if threshold <= value <= data_max:
        normed_value = (value - threshold) / (data_max - threshold)
        r = int(144 * (1 - normed_value) + 128 * normed_value)
        g = int(238 * (1 - normed_value) + 0 * normed_value)
        b = int(144 * (1 - normed_value) + 128 * normed_value)

    # 正常化值到 [data_min, threshold] 区间,从红(255,0,0)到黄(255,255,0)
    elif -10 <= value < threshold:
        normed_value = (value + 10) / (threshold + 10)  # 正则化到 [0, 1] 区间
        r = int(222 + (241 - 222) * normed_value)  # 从 222 渐变到 241
        g = int(28 + (147 - 28) * normed_value)  # 从 28 渐变到 147
        b = int(49 + (156 - 49) * normed_value)  # 从 49 渐变到 156

    elif data_min <= value < -10:
        r, g, b = 139, 0, 0

    else:
        r, g, b = 255, 255, 255  # 默认为白色

    hex_color = 'FF' + '{:02X}{:02X}{:02X}'.format(r, g, b)
    return hex_color


def fc_color_map(value, min_threshold, max_threshold, data_min, data_max):
    if min_threshold <= value <= max_threshold:
       
        normed_value = (value - min_threshold) / (max_threshold - min_threshold)

        hue = normed_value * 360

        r, g, b = colorsys.hsv_to_rgb(hue / 360.0, 1, 1)  # Here saturation and value are both set to 1
        r, g, b = int(r * 255), int(g * 255), int(b * 255)

        hex_color = 'FF' + '{:02X}{:02X}{:02X}'.format(r, g, b)

    # For values outside the specified range
    elif value > max_threshold or value < min_threshold:
        r, g, b = 139, 0, 0
        hex_color = 'FF' + '{:02X}{:02X}{:02X}'.format(r, g, b)
    else:
        r, g, b = 255, 255, 255  # default to white
        hex_color = 'FF' + '{:02X}{:02X}{:02X}'.format(r, g, b)
    return hex_color
def save_to_excel(df, threshold, output_path, color_function):
    wb = Workbook()
    ws = wb.active

    data_min = df.min().min()  # 获取整个 dataframe 中的最小值
    data_max = df.max().max()  # 获取整个 dataframe 中的最大值

    for i in range(df.shape[0]):
        for j in range(df.shape[1]):
            value = df.iloc[i, j]
            if not pd.isna(value):
                cell = ws.cell(row=i + 2, column=j + 2)
                cell.value = value
                # 选择正确的颜色映射函数和参数
                if color_function == color_map:
                    fill_color = color_function(value, threshold, data_min, data_max)
                elif color_function == fc_color_map:
                    min_threshold, max_threshold = threshold  # 从元组中解包
                    fill_color = color_function(value, min_threshold, max_threshold, data_min, data_max)

                cell.fill = PatternFill(start_color=fill_color,
                                        end_color=fill_color,
                                        fill_type="solid")
    wb.save(output_path)

def rgb_to_hex(rgb):
    return '{:02X}{:02X}{:02X}'.format(rgb[0], rgb[1], rgb[2])


def main():
    print("Starting main function...")
    input_file = filedialog.askopenfilename(title="Select the CSV file")
    print(f"Selected file: {input_file}")
    if "il" in input_file.lower():
        threshold_type = "IL"
        color_function = color_map
        threshold = get_threshold(threshold_type)
    else:
        threshold_type = "FC"
        color_function = fc_color_map
        threshold = get_threshold(threshold_type)
    if threshold is None:
        print("Invalid threshold. Exiting program.")
        return

    output_file = filedialog.asksaveasfilename(title="Save the visualization as", defaultextension=".xlsx",
                                               filetypes=[("Excel files", "*.xlsx")])
    print(f"Output file: {output_file}")

    df = pd.read_csv(input_file)
    save_to_excel(df, threshold, output_file, color_function)

if __name__ == "__main__":

main()






审核编辑:刘清

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • python
    +关注

    关注

    53

    文章

    4709

    浏览量

    83711

原文标题:用python对生成的map图上色

文章出处:【微信号:处芯积律,微信公众号:处芯积律】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    何用强度生成这样的云图!

    何用强度生成这样的云图!
    发表于 07-27 13:29

    请教各位大神如何用matlab画电机map???

    请教各位大神如何用matlab画电机map???小弟感激不尽。。。。。
    发表于 04-11 15:50

    python调用labview生成的dll

    何用python调用labview生成的dll
    发表于 02-03 15:59

    何用Proteus ISIS软件进行原理设计及仿真

    Proteus ISIS软件具有哪些功能?如何用Proteus ISIS软件进行原理设计及仿真?有哪些流程?
    发表于 11-10 06:34

    如何让Keil生成map文件

    一、要让Keil生成map文件,要设置:再重新编译,没有错误后,就会生成map文件了。二、map文件中相关概念:段(section) :描述
    发表于 11-23 06:54

    何用树莓派和Python去实现nRF24L01模块功能

    何用树莓派和Python去实现nRF24L01模块功能?其相关代码该如何去实现
    发表于 12-16 07:47

    请问arm必须要对生成的汇编指令进行优化吗

    请问在用arm neon指令优化程序时,在一个for循环下,分别用int32x2_t和int32x4_t类型的指令,后者的速度并没有按照理论上的速度更快,反而比前者慢是怎么回事?必须要对生成的汇编指令进行优化吗?谢谢指教。
    发表于 09-01 15:47

    请问arm必须要对生成的汇编指令进行优化吗

    请问在用ARM neon指令优化程序时,在一个for循环下,分别用int32x2_t和int32x4_t类型的指令,后者的速度并没有按照理论上的速度更快,反而比前者慢是怎么回事?必须要对生成的汇编指令进行优化吗?
    发表于 10-18 11:23

    详解如何用AD生成Gerber文件

    详解如何用AD生成Gerber文件
    发表于 11-23 11:07 0次下载

    STM32的hex文件和map文件如何生成

    的对话框中选择“Output”选项卡,然后勾选“Create HEX file”3、 生成map文件:选择“Listing”选项卡,勾选“Linker Listing: .\Listings\xxxxxxx.map”,并全选其下
    发表于 12-27 18:36 5次下载
    STM32的hex文件和<b class='flag-5'>map</b>文件如何<b class='flag-5'>生成</b>

    何用python爬取抖音app数据

    记录一下如何用python爬取app数据,本文以爬取抖音视频app为例。
    的头像 发表于 03-16 09:07 4768次阅读

    【C语言应用】如何用C代码生成一维码?

    【C语言应用】如何用C代码生成一维码?
    的头像 发表于 08-25 12:42 2192次阅读
    【C语言应用】如<b class='flag-5'>何用</b>C代码<b class='flag-5'>生成</b>一维码?

    Python怎么批量生成PDF文档

    这种模板套用的场景下,使用Python进行自动化就尤为方便,用最短的时间办最高效的事。 今天就给大家讲讲如何用Python自动套用模板批量生成
    的头像 发表于 02-28 10:11 812次阅读
    <b class='flag-5'>Python</b>怎么批量<b class='flag-5'>生成</b>PDF文档

    何用Python自动套用模板批量生成PDF文档

    今天就给大家讲讲如何用Python自动套用模板批量生成的PDF文档。 1.准备 开始之前,你要确保Python和pip已经成功安装在电脑上噢,如果没有,请访问这篇文章: 超详细
    的头像 发表于 10-17 10:54 629次阅读
    如<b class='flag-5'>何用</b><b class='flag-5'>Python</b>自动套用模板批量<b class='flag-5'>生成</b>PDF文档

    何用Python自动套用模板批量生成PDF文档

    办最高效的事。 今天就给大家讲讲如何用Python自动套用模板批量生成下方这样的PDF文档。 1.准备 开始之前,你要确保Python和pip已经成功安装在电脑上噢,如果没有,请访问这
    的头像 发表于 10-31 10:56 870次阅读
    如<b class='flag-5'>何用</b><b class='flag-5'>Python</b>自动套用模板批量<b class='flag-5'>生成</b>PDF文档