前幾期,有朋友讓我用python將cp的測(cè)試數(shù)據(jù)轉(zhuǎn)化為map
但是,他又想把特定的測(cè)量數(shù)據(jù)轉(zhuǎn)化為map圖后,進(jìn)行上色,即不同的測(cè)試數(shù)據(jù)能夠呈現(xiàn)不同的顏色,以便于直觀的觀察其趨勢(shì)。
數(shù)據(jù)樣式:
左邊列是序號(hào),中間列是XY,X-0016Y0079表示的是(X,Y)坐標(biāo)為(16,79),最右行是測(cè)試數(shù)據(jù)。序號(hào)最大值為13278,即這個(gè)wafer有13278粒完成測(cè)試,得到了cp的測(cè)試數(shù)據(jù)。
我的思路:
1,將現(xiàn)有數(shù)據(jù)按照坐標(biāo),轉(zhuǎn)化為map圖,并將測(cè)試數(shù)值一一對(duì)應(yīng)填入map圖中。
2,有時(shí)測(cè)試標(biāo)準(zhǔn)是不一樣的,所以可以手動(dòng)設(shè)置標(biāo)準(zhǔn)值,這個(gè)在運(yùn)行程序時(shí)能夠彈出對(duì)話框,讓使用者能夠隨意更改標(biāo)準(zhǔn)。低于標(biāo)準(zhǔn)值的測(cè)量值單元格呈現(xiàn)淺紅色到紅色的漸變,高于標(biāo)準(zhǔn)值的測(cè)量值呈現(xiàn)淺綠到紫色的漸變。
做出的效果:
1,雙擊“map上色.exe”運(yùn)行程序
2,選擇要上色的測(cè)試數(shù)據(jù)文件
3,手動(dòng)設(shè)置標(biāo)準(zhǔn)值
4,設(shè)置文件名
5,在原文件地址下輸出包含map圖的文件
6,打開文件
這樣就很直觀地看出測(cè)量值的分布圖來了。
原代碼如下,歡迎參考:
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] 區(qū)間,從極淡綠(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] 區(qū)間,從紅(255,0,0)到黃(255,255,0) elif -10 <= value < threshold: normed_value = (value + 10) / (threshold + 10) # 正則化到 [0, 1] 區(qū)間 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 # 默認(rèn)為白色 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() # 獲取整個(gè) dataframe 中的最小值 data_max = df.max().max() # 獲取整個(gè) 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 # 選擇正確的顏色映射函數(shù)和參數(shù) 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
+關(guān)注
關(guān)注
55文章
4767瀏覽量
84376
原文標(biāo)題:用python對(duì)生成的map圖上色
文章出處:【微信號(hào):處芯積律,微信公眾號(hào):處芯積律】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論