MATLAB新手求助:如何分析30年CO₂数据并关联温度变化绘图
Hey there! As someone who’s fumbled through MATLAB data workflows as a beginner, I’ll walk you through every step clearly—from getting your data into MATLAB to making meaningful plots that show the CO₂-temperature link. Let’s dive in!
Step 1: 导入数据集
Most climate datasets come in CSV or Excel format, so we’ll use MATLAB’s built-in tools to pull them in easily:
- If your data is a CSV file:
Use thereadtable()function—it’s beginner-friendly and keeps column labels intact. Run this in the Command Window or a script:
After running this, type% Replace 'co2_temp_data.csv' with your actual filename data_table = readtable('co2_temp_data.csv');data_tablein the Command Window to see your data—you’ll spot column names (like 'Year', 'CO2_ppm', 'Global_Temp_Anomaly') and rows of data. - If your data is an Excel file:
Usereadtable()with the filename extension:data_table = readtable('co2_temp_data.xlsx');
Pro tip: Make sure your dataset is saved in MATLAB’s current working directory (check this with pwd in the Command Window—if not, use cd to navigate to the folder with your data).
Step 2: 数据识别与预处理
Now that your data is loaded, let’s make sure it’s ready for analysis:
- Check column names: Run
data_table.Properties.VariableNamesto confirm which columns hold CO₂ levels, temperature data, and years. Let’s assume we have three key columns:Year,CO2_ppm,Global_Temp_C. - Handle missing values (super common in climate data):
If you seeNaNvalues, you can either remove those rows or fill them in. For simplicity, let’s remove rows with missing data:cleaned_data = rmmissing(data_table); - Extract key variables: Pull out the columns we need into separate arrays for easier plotting:
years = cleaned_data.Year; co2_levels = cleaned_data.CO2_ppm; temp_anomalies = cleaned_data.Global_Temp_C;
Step 3: 绘制折线图(CO₂与温度趋势)
Line charts are perfect for showing trends over time. Let’s make two separate plots first, then combine them into a dual-axis chart to see the correlation.
3.1 绘制CO₂浓度折线图
figure; % Creates a new figure window plot(years, co2_levels, 'b-', 'LineWidth', 2); xlabel('Year'); ylabel('CO₂ Concentration (ppm)'); title('近30年全球CO₂浓度趋势'); grid on; % Adds grid lines for readability
The 'b-' means a blue solid line, and 'LineWidth' makes the line thicker so it’s easier to see.
3.2 绘制温度异常折线图
figure; plot(years, temp_anomalies, 'r-', 'LineWidth', 2); xlabel('Year'); ylabel('Global Temperature Anomaly (°C)'); title('近30年全球温度异常趋势'); grid on;
3.3 双Y轴图(关联CO₂与温度)
To see how CO₂ levels track with temperature, let’s make a single plot with two Y-axes:
figure; ax1 = gca; % Get the first axis plot(years, co2_levels, 'b-', 'LineWidth', 2); xlabel('Year'); ylabel('CO₂ Concentration (ppm)', 'Color', 'b'); title('CO₂浓度与温度异常关联趋势(近30年)'); grid on; % Add the second Y-axis for temperature ax2 = axes('Position', ax1.Position, 'YAxisLocation', 'right', 'Color', 'none'); plot(ax2, years, temp_anomalies, 'r-', 'LineWidth', 2); ylabel('Global Temperature Anomaly (°C)', 'Color', 'r'); linkaxes([ax1, ax2], 'x'); % Makes sure both axes share the same X-axis (years)
This plot lets you see how rising CO₂ lines up with increasing temperature anomalies at a glance.
Step 4: 绘制柱状图(可选:年度CO₂增量)
If you want to show annual changes in CO₂ instead of overall trend, a bar chart works great:
% Calculate annual CO₂ increments co2_increment = diff(co2_levels); % We need to adjust years since diff reduces the array length by 1 years_increment = years(2:end); figure; bar(years_increment, co2_increment, 'FaceColor', 'teal'); xlabel('Year'); ylabel('Annual CO₂ Increment (ppm)'); title('近30年CO₂年度增量'); grid on;
Quick Troubleshooting Tips
- If MATLAB can’t find your data: Double-check the filename spelling and make sure the file is in your current working directory (use
dirto list files in the folder). - If plots look messy: Adjust the figure size with
figure('Position', [100 100 800 500])to make it wider/taller. - If you get errors with variable names: Make sure you’re using the exact column names from your dataset (check with
data_table.Properties.VariableNames).
内容的提问来源于stack exchange,提问作者Ahmed E

