1

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?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Vroni
  • 347
  • 1
  • 5
  • 16

3 Answers3

1

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')
yoonghm
  • 4,198
  • 1
  • 32
  • 48
0

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

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')
Polkaguy6000
  • 1,150
  • 1
  • 8
  • 15
  • Yes, this is a start. But now I get the windrose plot overlayed over a square empty figure... my code is the same, only added the line: fig,ax2 = plt.subplots(1,1) and changed the line: ax2 = WindroseAxes.from_ax() to ax2 = WindroseAxes.from_ax(fig=fig).. I also tried ax2 = WindroseAxes.from_ax(ax = ax2) instead but then I get an AttributeError: Unknown property normed... if I delete the 'normed' attribute from ax2.bar(..) I get AttributeError: Unknown property opening... – Vroni Jan 08 '19 at 15:13