【科研绘图 基础版】01 使用Python绘制时间序列折线图

       下面这段代码绘制了一个折线图,其中包含了实际平均温度数据和使用线性回归模型预测的平均温度数据(用来近似地表示数据的整体趋势)。

        具体来说,图中的横轴表示年份,纵轴表示平均温度。蓝色的实心线代表了实际的平均温度数据,而红色的实线则代表了使用线性回归模型所预测的平均温度数据。通过这个图,可以直观地比较实际数据和模型预测数据之间的趋势和关系。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
import matplotlib.ticker as ticker

# 读取CSV文件
data = pd.read_csv(r'F:\Annual LST.csv')

# 去除年份列中的逗号,并转换为浮点数
data['Year'] = data['Year'].str.replace(',', '').astype(float)

# 提取第一列和第二列数据
years = data.iloc[:, 0].values.reshape(-1, 1)
mean_temps = data.iloc[:, 1].values

# 创建线性回归模型
model = LinearRegression()

# 拟合数据
model.fit(years, mean_temps)

# 提取斜率
slope = model.coef_[0]

# 计算预测值
predicted_temps = model.predict(years)

# 创建折线图
plt.figure(figsize=(10, 6))
plt.plot(years, mean_temps, marker='o', markersize=8, linestyle='-', color='blue', label='Actual Mean Temperature')
plt.plot(years, predicted_temps, linestyle='-', color='red', label='Predicted Mean Temperature')
plt.xlabel('Year', fontsize=14)
plt.ylabel('Mean Temperature', fontsize=14)
plt.tick_params(axis='both', which='both', direction='in', right=True, top=True)

# 设置横坐标整数刻度
plt.gca().xaxis.set_major_locator(ticker.MaxNLocator(integer=True))

plt.tight_layout()
plt.show()

图 绘制的折线图

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中可以使用matplotlib和pandas模块来实现数据可视化折线图折线图常用于展示时间序列数据的趋势。在matplotlib模块中,可以使用plot方法来绘制折线图。当然,也可以使用pandas模块的plot方法来绘制折线图。 下面是使用pandas模块绘制折线图的具体代码示例: ```python import pandas as pd import matplotlib.pyplot as plt # 设置绘图风格 plt.style.use('ggplot') # 处理中文乱码 plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 读取数据 data = pd.read_excel(r'weather.xlsx') # 统计每月的平均最高气温 data = data.pivot_table(index='month', columns='year', values='high') # 绘制折线图 data.plot(kind='line', style=['-', '--', ':']) # 修改坐标轴标签 plt.xlabel('月份') plt.ylabel('气温') # 添加图形标题 plt.title('每月平均最高气温波动趋势') # 显示图形 plt.show() ``` 通过以上代码,可以绘制出每月平均最高气温的折线图,其中每年的数据使用不同的线条样式进行区分。这样可以更直观地展示出数据的变化趋势。 更多关于使用matplotlib模块绘制折线图的详细信息,可以参考Matplotlib的官方文档。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python数据可视化的例子——折线图(line)](https://blog.csdn.net/weixin_48615832/article/details/108466201)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [001 Python数据可视化之折线图](https://blog.csdn.net/qixinxiangshicheng/article/details/129480075)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值