Matplotlib Wind Direction Script

Posted on
Wed May 04, 2016 11:30 am
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Matplotlib Wind Direction Script

If you would rather not use the 360 individual compass needle images required for the WUnderground Gauge Pack wind direction gauge, you can use this Matplotlib script (optimized for the gauge pack at 100x100 pixels.) It requires Matplotlib (obviously) and numpy. The result is pretty presentable but doesn't share the 3D effect of the Gauge Pack needle. For those that are using flat images (non-skewmorphic) the result is just right.

Screen Shot 2016-05-04 at 12.14.48 PM.png
Screen Shot 2016-05-04 at 12.14.48 PM.png (17.3 KiB) Viewed 1938 times

Code: Select all
#! /usr/bin/env python2.6
# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np

# ******* User Settings ******
image_size         = 100      # <--- square, in pixels
pointer_color      = 'red'    # <--- can be 'black', '#000000', or rgb (000, 000, 000)
save_file_location = '/Users/username/Desktop/wind_dir.png'
wind_direction     = indigo.devices[indigo_device_id].states['windDegrees']
# ****************************

wind_direction_rad = np.radians(float(wind_direction))
plt.figure(figsize=(image_size / 100.0, image_size / 100.0))
ax = plt.subplot(111, polar=True)
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.spines['polar'].set_visible(False)
ax.plot((0, wind_direction_rad), (0, 5), color=pointer_color, linewidth=3, solid_capstyle='round')
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
plt.savefig(save_file_location, transparent=True, dpi=100)
plt.close()

The resulting image is transparent (save the needle itself) and can be used regardless of whether you use the Gauge Pack or not.

I would recommend running the script from a trigger whenever the underlying data changes, and adding the image as a refreshing image URL (so that it can change when the underlying data changes without having to reload the control page.) It should work properly with any data that is constrained 0°-360°--and I believe that values greater than 360 will just start another trip around.

With the proper math, this script can be adapted for the other gauges as well. You would just need to convert your potential values across the range of degree values (something in the range of 225° - 135° I think.)

Enjoy,
Dave

[EDIT] moved to the Dave's Scripts forum.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest