0

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, i.e.

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!

E.Wulf
  • 1
  • 1
  • 1
  • 1
    Imho `weather` or `graph` are irrelevant tags here. I would rather use `matplotlib` and `pandas`. – Mr. T Feb 15 '18 at 21:18

2 Answers2

0

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()
Karl Anka
  • 2,529
  • 1
  • 19
  • 30
  • Hi Karl, thanks for replying so fast! Unfortunately pandas isn't something I was taught to use in my basic coding classes but looks like something that would work well for this! However when I use this method I encounter an error message: 'ValueError: Expected 14 fields in line 2, saw 15'... any idea why this may be happening? – E.Wulf Feb 17 '18 at 17:17
  • As the error message say there seems to be 15 items in the second line of your text, where it was only 14 in the first line. I guess it has something to do with the separators between the columns in your file. Check this answer on how to read from text/csv into pandas: https://stackoverflow.com/questions/21546739/load-data-from-txt-with-pandas I dont know if you have any column names in your text-file. If not `read_csv` provides a a nice feature for adding column names while reading the file. – Karl Anka Feb 17 '18 at 18:21
0

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

alpha_989
  • 4,882
  • 2
  • 37
  • 48
  • Hi alpha_989, thanks for the reply! This does indeed provide a plot from my data, however I have noticed that it plots the data as all the 1st day data for each month, then all the 2nd day data from each month etc... (if that makes sense?). What I would like is, essentially, Jan 1st, 2nd, 3rd,... then onto February data, through to the December (last column) data. Any ideas? Thanks again! – E.Wulf Feb 17 '18 at 17:22