3

I am trying to make a wind rose from a series of windspeed and direction values. I have an idea of how to write the raw program for doing this as shown below:

from windrose import WindroseAxes
from matplotlib import pyplot as plt
import matplotlib.cm as cm
import numpy as np
ws=[2.6,2.3,2.1,2.0,2.1,2.2,2.9,2.8,2.39,1.90,1.54,1.29,0.72,0.18,1.08]
wd=[207,208,215,217,213,209,203,195,187,179,164,139,117,101,280]
print "WD is ",wd
print "WS is ",ws
ax = WindroseAxes.from_ax()
ax.bar(wd,ws, normed=True, opening=0.8, edgecolor='white')
ax.set_legend()
plt.show()

The only issue is is how can I have the program reading my data into the arrays ws (windspeed) and wd (wind direction) in the above program.

The data is in an ascii file with two columns separated by a space. The first column is wind speed and the second column is wind direction.

With wind speed in the first column and wind direction in the second column. Do you know how to read this type of wind using python so that column one occupies the ws array and column two occupies the wd array in the script above?

jms1980
  • 785
  • 1
  • 12
  • 31

3 Answers3

1

You can use np.loadtxt:

data = np.loadtxt('data.txt')
ws = data[:, 0]
wd = data[:, 1]
Ophir Carmi
  • 1,953
  • 1
  • 20
  • 38
1

Let's say you have a file named data.csv with the folowing content

2.6 207
2.3 208
2.1 215
2.0 217
2.1 213
2.2 209
2.9 203
2.8 195
2.39 187
1.90 179
1.54 164
1.29 139
0.72 117
0.18 101
1.08 280

You can use Pandas to read CSV file

df = pd.read_csv("data.csv", names=["ws", "wd"], sep=" ")
ws = df["ws"].values
wd = df["wd"].values

PS: windrose can also directly use Pandas DataFrame

scls
  • 12,653
  • 9
  • 36
  • 49
0

in above code there is something wrong:

ax = WindroseAxes.from_ax(): WindroseAxes has no attribute 'from_ax'

and also

ax.bar(wd,ws, normed=True, opening=0.8, edgecolor='white'): bar() missing 1 required positional argument: 'var'

have a close look on it

Vickel
  • 6,356
  • 6
  • 30
  • 49
epiphan
  • 1
  • 1