不会写代码也能做科研数据可视化?
2026/7/17 5:30:07 网站建设 项目流程

写在前面

做科研的人,几乎都经历过这种时刻:

手里有一份几千行的数据集,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)

这是所有可视化的起点,也是最容易被忽视的环节。

很多人拿到数据就急着画图——但其实你根本不知道这份数据在"说什么"。字段的含义是什么?数据的完整性如何?有没有明显的异常值?不同字段之间有什么关联?

传统做法,你需要:

  1. 打开Excel,手工滚动查看几百行数据
  2. df.info()看数据类型
  3. df.describe()看数值字段的统计分布
  4. 用各种图表逐一探索……

这是一个极其低效且容易遗漏的过程

书中通过**英国可再生能源规划数据库(REPD)**的案例展示了LLM辅助数据探索的威力。这个数据库有14,000+条可再生能源项目记录,涉及:

  • 技术类型(Solar、Wind、Hydro等15种)
  • 装机容量(从几十kW到1GW+)
  • 规划状态(从Permitted到Operational等5种阶段)
  • 地理位置(英国各地区)
  • 申请与完工年份(时间跨度20+年)

一个关键的Prompt就能做到的事:

Here's 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在这个环节的价值特别大,因为:

  1. 拼写不一致— 同一个分类可能有多种写法

    • "Solar Photovoltaic" vs "Solar PV" vs "solar pv" vs "Solar"
    • LLM可以快速识别这些变种并提出统一方案
  2. 日期格式混乱— 不同来源的数据,日期格式不统一

    • "01/12/2020" vs "2020-12-01" vs "Dec 1, 2020"
    • LLM生成的Python脚本可以用pd.to_datetime()统一转换
  3. 聚合和透视— 需要把数据从细粒度聚合到更高层级

    • 从单个项目层级 → 按技术类型聚合 → 按年份聚合
    • 生成交叉透视表(Pivot Table)用于可视化

案例——脱欧投票支出数据的清洗:

原始数据有12,000+条支出记录,但支出类别字段的数据质量很差:

  • "Campaign leaflets"、"campaign leaflets"、"Campaign Leaflets" 并存
  • "Surveys & polling"、"Surveys and polling"、"survey and polling" 混用
  • 某些支出类别只有1-2条记录,应该被合并到"其他"

一个精准的Prompt就能生成一个完整的清洗脚本:

Here's 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的价值正在这里:它可以帮你快速想清楚你想表达什么,进而推荐合适的图表。

一个标准的图表推荐Prompt:

Here's 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

预期输出会包括:

  1. Stacked Area Chart— 最优推荐

    • 优点:展示总体趋势+各技术相对增长
    • 缺点:小类别可能难以看清
  2. Multi-line Chart with Hierarchy— 次优

    • 优点:精确数值易读
    • 缺点:线条多容易混乱
  3. 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 doesn't 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. 你想看什么样的关系?相关性?差异?
  2. 目标受众是谁?
  3. 有没有特定的宗教或职业你特别感兴趣?

第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 I'm 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(可直接执行):

Here's 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的关键特征:

  1. 明确的数据结构— LLM知道它在处理什么
  2. 清晰的分析逻辑— 不是"找关系"而是"计算比率"
  3. 具体的输出规格— 哪些行/列、排序方式、颜色映射
  4. 技术约束— 使用什么库
  5. 易于验证— 输出结果你可以一眼看出是否合理

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: [粘贴或列举] Here's 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 doesn't 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能为你释放的时间和精力。

而这些被释放的时间和精力,可以用在更高价值的地方:深化研究、探索新问题、改进实验设计。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询