不会写代码也能做科研数据可视化?
写在前面做科研的人几乎都经历过这种时刻手里有一份几千行的数据集Excel打开之后面对密密麻麻的字段第一反应是——我该从哪里开始看然后你打开Python想写几行Pandas代码探索一下结果光想起groupby的具体语法就花了十分钟。你去查Stack Overflow粘贴代码发现报了个字段名不匹配的错误。又调了半小时。等你终于画出第一张图已经是三小时后的事了。而图还不一定好看。这件事的本质不是你不够努力而是数据可视化的学习曲线太陡了。在真正的数据洞察和你之间横亘着Excel的操作逻辑、Python的语法记忆、各种可视化库的API文档以及无数次为什么这里报错。这个门槛让很多本可以从数据中获得真知灼见的科研人止步于数字的表面。LLM的出现究竟如何从底层改变了数据探索与可视化的工作方式大型语言模型的出现对数据可视化从业者的工作产生了深远影响。它或许并没有改变什么是可能的但它让很多任务变得更容易、更快速。具体到数据可视化领域这句话意味着一个不会Python的科研人现在可以用自然语言让AI生成可视化代码自己运行和调整一个会Python的数据分析师可以把原本需要查文档的重复劳动外包给AI把精力集中在判断和设计上一个资深可视化工程师可以用AI快速探索自己不熟悉的图表类型和库加速创新不同水平的人都从AI这里获得了不同层次的解放。传统方式 vs LLM方式一张表说清楚维度传统方式LLM辅助方式用户输入工具特定命令或点击按钮自然语言提问所需专业度中等至高度需学习工具语法较低无需记忆语法速度与流畅度较慢容易打断探索节奏较快支持自然的探索流错误来源用户输入错误命令或分析失误Prompt不精准或不清晰数据处理方式工具直接处理生成代码如Python由用户审查后执行这张表揭示了一个关键的范式转移错误来源从语法记忆转移到了表达清晰度。传统方式下你犯错误是因为记不住df.groupby(column).agg({value: sum})的写法。LLM辅助方式下你犯错误是因为没有把你的问题说清楚。前者是技术门槛后者是表达能力。而对于科研人来说把问题说清楚本来就是我们最基本的能力之一——我们每天在写论文、写报告、做汇报表达本就是我们的母语。这个范式转移天然对科研人有利。四大支柱第一支柱数据探索Data Exploration这是所有可视化的起点也是最容易被忽视的环节。很多人拿到数据就急着画图——但其实你根本不知道这份数据在说什么。字段的含义是什么数据的完整性如何有没有明显的异常值不同字段之间有什么关联传统做法你需要打开Excel手工滚动查看几百行数据用df.info()看数据类型用df.describe()看数值字段的统计分布用各种图表逐一探索……这是一个极其低效且容易遗漏的过程。书中通过**英国可再生能源规划数据库(REPD)**的案例展示了LLM辅助数据探索的威力。这个数据库有14,000条可再生能源项目记录涉及技术类型Solar、Wind、Hydro等15种装机容量从几十kW到1GW规划状态从Permitted到Operational等5种阶段地理位置英国各地区申请与完工年份时间跨度20年一个关键的Prompt就能做到的事Heres my dataset with columns: [粘贴字段名称和数据类型] And here are 10 sample rows: [粘贴样本数据] Please give me an overview: 1. What does each field represent? 2. What are the data ranges and types? 3. How complete is the data? (missing values per column) 4. What patterns do you notice in the sample? 5. What are obvious next questions to explore?输出质量能达到的高度在不到30秒内LLM会返回给你14,000条记录实际上分布在5种规划状态其中Permitted占比最高Solar和Wind两种技术占总装机容量的87%2010-2019年是项目申报的高峰期2020年后明显下滑可能与英国脱欧有关某些地区的数据缺失率较高这些都是后续可视化设计的关键输入信息。你现在已经大致知道了这份数据讲的故事接下来的可视化就不是盲目探索而是有目的的表达。第二支柱数据清洗与转换Data Transformation这是工作量最大却最容易被低估的环节。数据科学家们常说80%的时间花在数据清洗上。书中强调LLM在这个环节的价值特别大因为拼写不一致— 同一个分类可能有多种写法Solar Photovoltaic vs Solar PV vs solar pv vs SolarLLM可以快速识别这些变种并提出统一方案日期格式混乱— 不同来源的数据日期格式不统一01/12/2020 vs 2020-12-01 vs Dec 1, 2020LLM生成的Python脚本可以用pd.to_datetime()统一转换聚合和透视— 需要把数据从细粒度聚合到更高层级从单个项目层级 → 按技术类型聚合 → 按年份聚合生成交叉透视表Pivot Table用于可视化案例——脱欧投票支出数据的清洗原始数据有12,000条支出记录但支出类别字段的数据质量很差Campaign leaflets、campaign leaflets、Campaign Leaflets 并存Surveys polling、Surveys and polling、survey and polling 混用某些支出类别只有1-2条记录应该被合并到其他一个精准的Prompt就能生成一个完整的清洗脚本Heres my dataset with a column called expenditure_type that has: - Inconsistent capitalization (Campaign Leaflets, campaign leaflets, etc) - Slight spelling variations (Surveys polling vs Surveys and polling) - ~50 unique values, but I suspect many are really just 8-10 categories Please write a Python script that: 1. Groups similar category names together 2. Maps each group to a standardized category name 3. For any categories with 3 occurrences, mark them as Other 4. Shows me the mapping dictionary so I can verify/adjust Output should be executable Python code.获得的代码会包含# 定义类别映射 category_mapping { Campaign leaflets: Print Materials, campaign leaflets: Print Materials, Campaign Leaflets: Print Materials, Surveys polling: Polling, Surveys and polling: Polling, # ... 更多映射 } # 应用映射 df[expenditure_type_clean] df[expenditure_type].map(category_mapping) # 处理未映射的值 unmapped df[df[expenditure_type_clean].isna()][expenditure_type].value_counts() # 对罕见类别进行二次清洗...关键洞察这不是AI帮你做决定而是AI帮你快速生成候选方案你再用专业判断调整。这是最健康的人机协作模式。第三支柱图表类型推荐Chart Recommendation很多可视化教程教你什么是柱状图什么是散点图但没人教你在你的具体数据面前应该选哪种图表。图表推荐的决策树第一层决策你想表达什么比较Comparison哪个大如何排序组成Composition这个整体由哪些部分组成占比如何趋势Trend随时间如何变化分布Distribution数据怎样分散关系Relationship两个变量如何关联第二层决策数据的维度和类型一个分类字段 → 简单柱状图多个分类字段 → 分面柱状图或Treemap时间序列 → 折线图或面积图地理数据 → 地图或地理散点图多维关系 → Sankey图或散点矩阵第三层决策受众和使用场景学术论文 → 静态高分辨率图表Matplotlib、R ggplot2内部演讲 → 交互式图表Plotly数据仪表盘 → 需要过滤和实时更新Streamlit、Dash书中通过REPD数据展示的推荐案例问题1「英国不同地区的可再生能源项目分布如何」表面上这是一个地理问题但关键在于是想比较各地区的项目数量→ 地图着色是想看各地区的装机容量→ 地图着色色深表示容量是想展示技术类型在各地区的组成→ 地图着色叠加饼图或堆积柱状图同一个问题选择不同图表完全不同。问题2「可再生能源技术的装机容量如何分布」简单答案柱状图按容量排序更好的答案Treemap— 既展示容量大小方块大小又展示占比面积比例最佳答案Sunburst图— 如果需要展示技术类型 子类型如Solar分为固定式和跟踪式的层级关系问题3「从2000年到2020年不同技术的项目申报趋势如何」简单答案多条折线按技术类型分别绘制更好的答案堆积面积图— 展示总体趋势同时看各技术的相对贡献最佳答案取决于受众想对比什么 → 是看绝对增长还是相对份额这就是为什么图表推荐这个环节值得单独用一章讨论— 它不是关于掌握多少种图表的知识而是关于清晰表达你的分析意图。而LLM的价值正在这里它可以帮你快速想清楚你想表达什么进而推荐合适的图表。一个标准的图表推荐PromptHeres my goal: I want to show that wind and solar technologies have grown significantly since 2010, and they now dominate the UK renewable energy landscape. My audience: non-technical stakeholders in a policy briefing. My data: 14,000 renewable energy projects with columns: - technology_type (15 categories) - application_date (2000-2020) - capacity_mw (numeric) Recommend 5 chart types for this goal, ranked by effectiveness. For each, explain: 1. Why it works for this message 2. Pros and cons 3. Implementation effort预期输出会包括Stacked Area Chart— 最优推荐优点展示总体趋势各技术相对增长缺点小类别可能难以看清Multi-line Chart with Hierarchy— 次优优点精确数值易读缺点线条多容易混乱Small Multiples (分面折线图)— 另一选项优点清晰看每种技术的独立趋势缺点不易对比总体规模第四支柱代码生成与迭代Code Generation Iteration核心问题是LLM生成的代码如何确保它是正确的这是一个被很多人忽视的严肃问题。LLM可以生成代码但LLM生成的代码不一定能运行也不一定能正确处理你的数据。几个关键验证环节1. 逻辑验证代码是否理解正确了你的需求字段名称是否与实际数据匹配数据类型转换是否正确示例问题你问AI生成一个按技术类型统计项目数量的代码AI给出的代码可能会混淆项目数量和总装机容量——这种错误AI无法自动发现需要你审查。2. 数据完整性验证缺失值是否被正确处理是否有意外的数据过滤或丢失例如如果你的日期字段有缺失某些时间序列的代码可能会无声地忽略这些记录。3. 输出质量验证图表是否清晰易读坐标轴标签是否有意义颜色选择是否合理迭代策略是先运行、看结果、再修改而不是在Prompt阶段反复调整。高效的迭代流程第1轮写一个保险的通用Prompt ↓ 第2轮运行代码看生成的图表 ↓ 第3轮如果有问题针对具体问题点反馈修改 而不是笼统地说这个不太对 ↓ 第4轮验证修改后的代码逻辑例如不要这样反复修改❌Prompt 1:Make a chart of renewable energy by technology→ AI生成柱状图❌Prompt 2:This chart doesnt look good→ AI无从下手✅应该这样❌Prompt 1:Make a bar chart where: - X-axis: technology type (sorted by capacity descending) - Y-axis: total installed capacity in MW - Color: light blue - Output: high-resolution PNG suitable for academic paper✅Prompt 2如果有具体问题:The chart looks good, but I notice Wind category includes both onshore and offshore, which should be separated. Can you split it into two bars (one for onshore, one for offshore)?一个完整的真实Prompt案例拆解用一个案例来展示如何从模糊需求逐步精炼成可执行Prompt。案例背景英国职业×宗教数据分析数据来源英国人口普查包含22种宗教分类60种职业分类英国各地区样本量百万级初始需求模糊版我想看看宗教信仰与职业选择有没有关系这个需求太模糊三个问题没有明确你想看什么样的关系相关性差异目标受众是谁有没有特定的宗教或职业你特别感兴趣第1版Prompt仍然不够精准I have UK census data with religion and occupation. Can you show me if religion and occupation are related?LLM的可能输出生成一个60×22的热力图太大难以看清或者生成一个混乱的气泡图或者问你你具体想看什么第2版Prompt逐步清晰I have UK census data with: - religion_category (22 categories like Christian, Jewish, Muslim, etc.) - occupation (60 categories) - count (number of people in each combination) - region (UK regions) The insight Im looking for: Are certain occupations (especially high-income professions like lawyers, doctors, finance) more common in certain religious communities than the UK average? My audience: policy researchers who understand statistics. Can you suggest the best visualization to highlight these overrepresentations/underrepresentations?LLM的高质量输出应该包括建议使用热力图— 展示每个宗教-职业组合相对于全国平均的偏离程度或者建议分面柱状图— 按宗教分类每个面显示该宗教中的职业分布或者建议平行集合图Parallel Sets— 展示宗教→职业的流向关系第3版Prompt可直接执行Heres my data structure: - CSV file with columns: religion, occupation, count, region - 22 unique religions, 60 occupations, 12 regions - Total 15,000 rows Goal: Create a heatmap showing which occupations are over/under-represented in each religious community compared to national average. Specific requirements: 1. Calculate for each (religion, occupation) pair: actual_percentage / national_average_percentage (This ratio 1 means overrepresented, 1 means underrepresented) 2. Show only occupations with at least 0.5% of the population (to avoid noise from rare combinations) 3. Show only religions with at least 1% of the population 4. Create a heatmap where: - Y-axis: religion (sorted by population size, largest first) - X-axis: top 20 occupations by representation ratio variation - Color: ratio value (red for overrepresented, blue for underrepresented) - Each cell shows the ratio value 5. Use Plotly or Matplotlib (Python) 6. Add a colorbar and title explaining what the ratio means Please generate the complete Python code that I can run on my CSV file.这版Prompt的关键特征✅明确的数据结构— LLM知道它在处理什么✅清晰的分析逻辑— 不是找关系而是计算比率✅具体的输出规格— 哪些行/列、排序方式、颜色映射✅技术约束— 使用什么库✅易于验证— 输出结果你可以一眼看出是否合理LLM生成的代码结构会大致是import pandas as pd import plotly.express as px # 读取数据 df pd.read_csv(census_data.csv) # 计算全国平均 national_avg df.groupby(occupation)[count].sum() / df[count].sum() # 计算各宗教的职业分布 religion_occupation df.groupby([religion, occupation])[count].sum() religion_totals df.groupby(religion)[count].sum() # 计算比率 ratio (religion_occupation / religion_totals.values) / national_avg.values # 过滤和排序 # 生成热力图 fig px.imshow(...)这个案例展示了一个重要的原则从模糊的分析意图到可执行的Prompt需要经过3-4轮的思维澄清而这个过程本身就是科研的核心 — 把问题说清楚。这就是为什么LLM不改变可能性但改变效率这句话这么精确。你的分析能力没变但你不用被语法细节绊住脚可以更快地落地想法。完整工具库直接可用的Prompt集锦 工具1数据概览Prompt用途快速获得数据集的全景认知Prompt模板I have a dataset with the following information: - File format: CSV - Total rows: [数字] - Column names and types: [粘贴或列举] Heres a sample of 5 rows: [粘贴样本数据至少3-5行] Please provide a data overview report with: 1. What does each field represent? (provide brief description) 2. Data types and value ranges for each column 3. Completeness: what percentage of data is missing in each column? 4. Data quality issues you notice (duplicates, inconsistencies, outliers) 5. What are the 3 most interesting questions this data could answer? 6. Any preprocessing or cleaning needed before visualization? Keep your analysis concise but thorough.预期输出质量5分钟内得到一份2000字的数据概览报告 工具2数据清洗Prompt用途生成自动化的数据清洗脚本Prompt模板I have a dataset with data quality issues in specific columns. Column: [列名] Issue type: [不一致的拼写 / 日期格式混乱 / 分类值过多 / 缺失值处理] Examples of the problem: [列举2-3个具体例子] Desired outcome: [描述你希望的标准化结果] Please write Python code (using pandas) that: 1. Identifies and groups similar values 2. Maps them to standardized names 3. Shows me the mapping dictionary for verification 4. Handles any edge cases I need the code to be safe and only process local data (no API calls). Output as executable Python script that I can run on my CSV file.示例应用Column: technology_type Issue: inconsistent capitalization and abbreviations Examples: - Solar Photovoltaic, solar photovoltaic, Solar PV, SOLAR_PV - Wind onshore, wind onshore, Wind (Onshore) - Hydroelectric, Hydro, hydro power Desired: standardized to 15 official categories 工具3图表推荐Prompt用途根据分析目标获得最优图表建议Prompt模板Analysis Goal: [你想用这个数据回答什么问题] Example: I want to show that renewable energy capacity has grown dramatically since 2010, and solar/wind now dominate Target Audience: [谁会看这个图表] Example: Policy makers and investors (non-technical) Data Overview: - [主要字段及其类型] - [数据规模] - [时间跨度或地理范围] Current Constraint or Preference: - [比如需要在论文中嵌入所以要高分辨率静态图 或要放在dashboard中需要交互功能] Based on this, recommend the 5 best chart types for this goal. For each recommendation, explain: 1. Chart name and why it fits this specific goal 2. How to structure the data (X/Y axes, colors, sizes, etc.) 3. Pros and cons for your audience 4. Estimated implementation time (simple / moderate / complex) 5. Any alternative library choices Rank them by effectiveness for your specific goal, not by general popularity.示例应用Analysis Goal: Show that UK renewable energy is heavily concentrated in specific geographic regions, and certain regions are falling behind Data: 14,000 projects with location, technology, capacity, status Audience: Local government officials Constraint: Need interactive map for presentations 工具4代码生成Prompt高级版用途从数据到完整可视化代码的一站式生成Prompt模板DATASET SPECIFICATION: - Source: [CSV/Excel/database] - File name: [如果适用] - Rows: ~[数字] | Columns: [数字] - Column names and types: * field_name_1 (type: date, range: 2000-2020) * field_name_2 (type: categorical, values: A, B, C, D) * field_name_3 (type: numeric, range: 0-1000) [... 其他字段 ...] VISUALIZATION REQUIREMENT: Type: [具体图表类型比如 Interactive treemap 或 Multi-line chart with legend] Specific design: - X-axis: [字段名] (how to sort/group?) - Y-axis: [字段名] (aggregation: sum/count/average/median?) - Color encoding: [字段名] (what does color represent?) - Interactivity: [比如 hover tooltip, click to filter] - Size/other dimensions: [如适用] CONSTRAINTS: - Library preference: [Plotly Express / Matplotlib / Altair / etc.] - Output format: [HTML / PNG / interactive dashboard] - Must work with local CSV (no external APIs) - Resolution: [if applicable, e.g., 300 dpi for print] DELIVERABLES: Please provide: 1. Complete, runnable Python code 2. Comments explaining each section 3. Any required imports and dependencies 4. Sample execution command 5. Brief explanation of data transformations applied [Optional] After generating the chart, I want to [specific follow-up like filter by technology type]这是最强大的Prompt模板— 输入足够详细LLM生成的代码通常可以直接运行。 工具5迭代反馈Prompt用途当第一版输出有问题时精准定位问题并修正Prompt模板The chart you generated earlier has an issue: SPECIFIC OBSERVATION: [描述问题而不是笼统地说这个不对] Example specific problems: ❌ The chart doesnt look good ✅ The Wind category shows 250 projects, but my raw data shows 380 wind projects. The aggregation seems wrong. ❌ The colors are confusing ✅ The red color for Hydro makes it hard to distinguish from the orange for Wind when printed in grayscale. Please use a colorblind-friendly palette. ❌ Make it better ✅ The legend is too large and covers part of the data area. Move it to the right side below the chart. DESIRED FIX: [明确说明怎样才是对的] VERIFICATION CHECK: Can you explain the fix and how I can verify the numbers are correct? 工具6数据隐私保护Prompt用途在保护敏感数据的前提下获得可用的分析脚本Prompt模板I have sensitive data that I cannot upload. However, I can share the structure. DATASET STRUCTURE (headers only): [粘贴列名和数据类型] SAMPLE DATA (completely anonymized, 3 rows): [粘贴样本行替换所有敏感信息] Example: Original: Patient ID: 12345, Age: 45, Diagnosis: Diabetes Anonymized: ID: [masked], Age: 40-50, Diagnosis: [masked] THE ANALYSIS TASK: [描述你想做什么分析] REQUEST: Please write Python code that: 1. Works entirely locally (no data transmission to cloud/API) 2. Handles the data structure as described 3. Produces the desired visualization 4. Does not require any external authentication or credentials I will run this code on my local machine with my actual data file. Can you provide the script without needing the actual sensitive data?关键安全检查✅ 确认代码只调用pandas,matplotlib,plotly等本地库✅ 确认没有requests或网络传输代码✅ 确认没有调用外部API 工具7跨语言代码转换Prompt用途在Python和R之间快速转换可视化代码Prompt模板I have Python code for a Matplotlib chart, and I need the equivalent R code using ggplot2. PYTHON CODE: [粘贴完整Python代码] REQUIREMENTS: - Use ggplot2 for visualization - Maintain the same data transformations - Produce visually similar output - Include all necessary library imports and data loading Please provide: 1. Complete R script 2. Explanation of any differences between Matplotlib and ggplot2 approaches 3. Any libraries I need to install反向也适用— 从R到Python或从Matplotlib到Plotly。关键数据指标用数字说话维度传统方式LLM辅助提升从数据到第一张可用图表的时间2-4小时15-30分钟8-16倍数据清洗脚本的生成时间1-2小时5-10分钟6-12倍图表迭代周期每次30分钟每次2-5分钟6-15倍需要掌握的编程知识中等Python/SQL较低自然语言门槛↓80%学习成本达到可用水平3-6个月1-2周8-12倍这些数字的含义对科研人的影响一个原本需要1周的数据分析项目现在1天可以完成一个需要聘请数据分析师的任务现在研究生可以独立完成一个论文中想尝试但太麻烦了的分析角度现在可以快速验证一个反思问题读完这篇文章问自己一个问题你上一次完成的数据分析项目中有多少时间浪费在记语法、调试代码、反复试图表上而不是在真正的分析和思考上这个比例就是LLM能为你释放的时间和精力。而这些被释放的时间和精力可以用在更高价值的地方深化研究、探索新问题、改进实验设计。