Python-绘制历史温度数据



我目前正在研究1815年的坦伯拉山爆发是如何造成所谓的"不夏季"的,并且需要一些帮助绘制数据的帮助。

我有一个来自气象站的文本文件,该文件提供了我感兴趣的一年(1816(的每日温度数据。文件中的每一列代表一个月,从一月到十二月(禁止第一行(,每行是本月的一天;

1  -22    5   52   82  102  155  176  168  100  114   89   54
2  -31   21   68  107  139  177  146  159   90  118   85   74
3  -49   41   63  103  170  134  144  140  106   99   86   63
4  -52   56   77  109  180  137  153  136  105  105   52   90
5  -66   75   67  103  169  165  160  145  102   90   62   74
6  -35   80   60   82  121  152  173  131  123   96   86   60
7  -17   69   34   91  128  175  195  125  139  103   75   65
8   -7   80  -17   79  152  161  135  134  148  104   34   64

我对Python是相对较新的,但是当然可以做基本的绘图...但是在这里显示的数据的方式使我有些困惑!我想绘制的是整整一年,X轴上的天数和Y轴上的温度值在一个图上。这样就可以让我今年与其他年份进行比较!

我可以做基本的事情,例如选择代表一月的列并将所有-999值设置为Nan,即

d = np.loadtxt("test.txt")
January = d[:,1]
January[January <= -999] = np.nan

,但很难考虑一种按我想要的方式绘制所有数据的方法。

任何帮助将不胜感激!

将文本文件读取到pandas dataframe中。然后,您可以解开它,该堆积将连续产生一系列的一系列。

import pandas as pd
# read text-file
df = pd.read_csv('test.txt', sep='  ')
# drop the first column in your text file
df = df.drop(df.columns[0], axis=1)
# unstack to make a series of the days
df = df.unstack()
# remove na that will come from months with less than 31 days
df = df.dropna()
# plot with pandas
df.plot()

这是这样做的代码:

import numpy as np
import matplotlib.pyplot as plt
temp_data = np.loadtxt("plot_weather_data.txt")
num_days = len(temp_data)
temperature = []
# for each of the days
for index_days in range(0, num_days-1):
#     for each of the months
    for index_month in range(1, 13):
#         starting from the second column, append the value to a list
        temperature.append(temp_data[index_days][index_month])
# call matplot lib and plot the graph
plt.plot(temperature)
plt.ylabel("temperature [C]")
plt.show()

您还可以在此处找到一个名为plot_weather_data.txt的文本文件:https://github.com/alphactzo7g/stackexchange/blob/master/master/python/plot_temp.py

最新更新