How to save a windrose plot

1.5k Views Asked by At

I plotted my wind data (direction and speed) with the windrose module https://windrose.readthedocs.io/en/latest/index.html. The results look nice but I cannot export them as a figure (png, eps, or anything to start with) because the result is a special object type that does not have a 'savefig' attribute, or I don't find it.

I have two pandas.core.series.Series: ff, dd

 print(ff)

result:

TIMESTAMP
2016-08-01 00:00:00    1.643
2016-08-01 01:00:00    2.702
2016-08-01 02:00:00    1.681
2016-08-01 03:00:00    2.208  
....

print(dd)

result:

TIMESTAMP
2016-08-01 00:00:00    328.80
2016-08-01 01:00:00    299.60
2016-08-01 02:00:00    306.90  
2016-08-01 03:00:00    288.60
...

My code looks like:

from windrose import WindroseAxes

ax2 = WindroseAxes.from_ax()
ax2.bar(dd, ff, normed=True, opening=0.8, edgecolor='white', bins = [0,4,11,17])
ax2.set_legend()
ax2.tick_params(labelsize=18)
ax2.set_legend(loc='center', bbox_to_anchor=(0.05, 0.005), fontsize = 18)
ax2.savefig('./figures/windrose.eps')
ax2.savefig('./figures/windrose.png')

But the result is:

AttributeError: 'WindroseAxes' object has no attribute 'savefig'

Do you know how to create a figure out of the result so I can use it in my work?

3

There are 3 best solutions below

1
Polkaguy6000 On

The error is occuring because you are trying to save the subplot instead of the figure. Try:

 fig,ax2 = plt.subplots(1,1) # Or whatever you need.
 # The windrose code you showed
 fig.savefig('./figures/windrose.png')
0
yoonghm On

We can use pyplot.savefig() from matplotlib.

import pandas as pd
import numpy as np
from windrose import WindroseAxes
from matplotlib import pyplot as plt
from IPython.display import Image

df_ws = pd.read_csv('WindData.csv')
# df_ws has `Wind Direction` and `Wind Speed`

ax = WindroseAxes.from_ax()
ax.bar(df_ws['Wind Direction'], df_ws['Wind Speed'])
ax.set_legend()

# savefig() supports eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff
plt.savefig('WindRose.jpg')
plt.close()

Image(filename='WindRose.jpg')
0
Reza_nadimi On

assuming that you are using box type of windrose module. Then the following code converts your wind rose into an image:

ax = WindroseAxes.from_ax()
ax.box(direction=wd, var=ws, bins=bins)
buff = io.BytesIO()
plt.savefig(buff, format="jpeg")
pixmap = QtGui.QPixmap()
pixmap.loadFromData(buff.getvalue())
dialog.ui.windrose_label.setScaledContents(True)
dialog.ui.windrose_label.setPixmap(pixmap)