时间序列--可视化的几种方式

一:随时间变化的线性曲线

除了最常见的,还可以设置分组,比如

from pandas import Series
from pandas import DataFrame
from pandas import TimeGrouper
from matplotlib import pyplot
series = Series.from_csv('daily-minimum-temperatures.csv', header=0)
groups = series.groupby(TimeGrouper('A'))
years = DataFrame()
for name, group in groups:
	years[name.year] = group.values
years.plot(subplots=True, legend=False)
pyplot.show()

Minimum Daily Temperature Yearly Line Plots每一行都是365天

二:直方图或者密度图

series.plot(kind='kde')--密度图

series.hist()--直方图

三:箱线图

series.boxplot()

下面给出一个年份不同月的箱线图

from pandas import Series
from pandas import DataFrame
from pandas import TimeGrouper
from matplotlib import pyplot
from pandas import concat
series = Series.from_csv('daily-minimum-temperatures.csv', header=0)
one_year = series['1990']
groups = one_year.groupby(TimeGrouper('M'))
months = concat([DataFrame(x[1].values) for x in groups], axis=1)
months = DataFrame(months)
months.columns = range(1,13)
months.boxplot()
pyplot.show()

Minimum Daily Temperature Monthly Box and Whisker Plots

四:热力图

现在横坐标代表每年中的某一天,纵坐标代表某一年,另外一个值大小就用颜色表示

from pandas import Series
from pandas import DataFrame
from pandas import TimeGrouper
from matplotlib import pyplot
series = Series.from_csv('daily-minimum-temperatures.csv', header=0)
groups = series.groupby(TimeGrouper('A'))
years = DataFrame()
for name, group in groups:
	years[name.year] = group.values
years = years.T
pyplot.matshow(years, interpolation=None, aspect='auto')
pyplot.show()

Minimum Daily Temperature Yearly Heat Map Plot

当然也可以画出月份和日期的热力图

from pandas import Series
from pandas import DataFrame
from pandas import TimeGrouper
from matplotlib import pyplot
from pandas import concat
series = Series.from_csv('daily-minimum-temperatures.csv', header=0)
one_year = series['1990']
groups = one_year.groupby(TimeGrouper('M'))
months = concat([DataFrame(x[1].values) for x in groups], axis=1)
months = DataFrame(months)
months.columns = range(1,13)
pyplot.matshow(months, interpolation=None, aspect='auto')
pyplot.show()

 Minimum Daily Temperature Monthly Heat Map Plot

五:滞后项和原项之间的点图

Pandas has a built-in function for exactly this called the lag plot. It plots the observation at time t on the x-axis and the lag1 observation (t-1) on the y-axis.

  • If the points cluster along a diagonal line from the bottom-left to the top-right of the plot, it suggests a positive correlation relationship.主对角线--正相关
  • If the points cluster along a diagonal line from the top-left to the bottom-right, it suggests a negative correlation relationship.次对角线--负相关
  • Either relationship is good as they can be modeled.

More points tighter in to the diagonal line suggests a stronger relationship and more spread from the line suggests a weaker relationship.越分散--越不相关

from pandas import Series
from matplotlib import pyplot
from pandas.tools.plotting import lag_plot
series = Series.from_csv('daily-minimum-temperatures.csv', header=0)
lag_plot(series)
pyplot.show()

Minimum Daily Temperature Lag Plot

这里只画出来t和t-1的图,当然也可以画t和t-1,t和t-2,t和t-3...t和t-7

from pandas import Series
from pandas import DataFrame
from pandas import concat
from matplotlib import pyplot
from pandas.plotting import scatter_matrix
series = Series.from_csv('daily-minimum-temperatures.csv', header=0)
values = DataFrame(series.values)
lags = 7
columns = [values]
for i in range(1,(lags + 1)):
	columns.append(values.shift(i))
dataframe = concat(columns, axis=1)
columns = ['t+1']
for i in range(1,(lags + 1)):
	columns.append('t-' + str(i))
dataframe.columns = columns
pyplot.figure(1)
for i in range(1,(lags + 1)):
	ax = pyplot.subplot(240 + i)
	ax.set_title('t+1 vs t-' + str(i))
	pyplot.scatter(x=dataframe['t+1'].values, y=dataframe['t-'+str(i)].values)
pyplot.show()

Minimum Daily Temperature Scatter Plots 

六:自相关图(和五的区别是这一步量化相关关系)

用的是pearson相关系数

from pandas import Series
from matplotlib import pyplot
from pandas.tools.plotting import autocorrelation_plot
series = Series.from_csv('daily-minimum-temperatures.csv', header=0)
autocorrelation_plot(series)
pyplot.show()

Minimum Daily Temperature Autocorrelation Plot

https://machinelearningmastery.com/time-series-data-visualization-with-python/ 

以下是对原代码的优化,主要解决了分割方式可能不准确的问题,使用正则表达式来匹配可能的分隔符,同时处理了可能出现的异常情况: ```python import os import re folder_path = 'E:\本基\新范围焚风强度\(新)选择数据v2\Tem_Rhu' for filename in os.listdir(folder_path): if filename.endswith('.txt'): file_path = os.path.join(folder_path, filename) daily_min_temperatures = {} try: with open(file_path, 'r', encoding='utf-8') as file: for line in file: # 使用正则表达式匹配可能的分隔符 parts = re.split(r'[ \t,;]+', line.strip()) if len(parts) >= 5: year = parts[0] month = parts[1] day = parts[2] try: temperature = float(parts[4]) date = f'{year}-{month}-{day}' if date not in daily_min_temperatures: daily_min_temperatures[date] = temperature elif temperature < daily_min_temperatures[date]: daily_min_temperatures[date] = temperature except ValueError: print(f"在文件 {filename} 中遇到无法转换为浮点数的温度值: {parts[4]}") except Exception as e: print(f"处理文件 {filename} 时出现错误: {e}") output_filename = f'{os.path.splitext(filename)[0]}_min_tem.txt' output_path = os.path.join(folder_path, output_filename) try: with open(output_path, 'w', encoding='utf-8') as output_file: for date, min_temp in daily_min_temperatures.items(): output_file.write(f'{date}\t{min_temp}\n') except Exception as e: print(f"写入文件 {output_filename} 时出现错误: {e}") ``` ### 优化点说明: 1. **分隔符处理**:使用 `re.split(r'[ \t,;]+', line.strip())` 来匹配可能的分隔符(空格、制表符、逗号、分号),避免了原代码中固定使用制表符分割的问题。 2. **异常处理**:添加了 `try-except` 块来捕获和处理可能出现的异常,如文件读取错误、温度值无法转换为浮点数等。 3. **编码指定**:在打开文件时指定了 `encoding='utf-8'`,避免编码问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值