简体   繁体   中英

Python - Plotting Historical Temperature Data

I am currently looking into how the 1815 Mount Tambora eruption caused the so-called "Year without a Summer" and need some help plotting data.

I have a text file from a weather station that provides daily temperature data for the year I am interested in (1816). Each column in the file represents the month, from January through to December, (bar the first one) and each row is the day in the month;

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

I am relatively new to Python, but can certainly do basic plotting... but the way the data is presented here has me slightly stumped! What I would like to plot is the whole year, with the days/months on the x-axis, and the temperature values on the y-axis, on one graph. This would then allow me to compare this year with other years!

I can do basic things like select the column that would represent January and set all -999 values to nan, ie

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

But struggle to think of a way to plot all the data the way I want it.

Any help would be much appreciated!

Read the text file into a pandas dataframe. Then you can unstack it which will yield a series of all days in a row.

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()

Heres the code to do that:

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()

You can also find a text file named plot_weather_data.txt along with the above file here: https://github.com/alphaCTzo7G/stackexchange/blob/master/python/plot_temp.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM