1

我用 windrose 模块https://windrose.readthedocs.io/en/latest/index.html绘制了我的风数据(方向和速度)。结果看起来不错,但我无法将它们导出为图形(png、eps 或任何开头的东西),因为结果是一个没有“savefig”属性的特殊对象类型,或者我找不到它。

我有两个 pandas.core.series.Series:ff,dd

 print(ff)

结果:

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)

结果:

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
...

我的代码如下所示:

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

但结果是:

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

你知道如何根据结果创建一个图形,以便我可以在工作中使用它吗?

谢谢!

4

3 回答 3

1

我们可以使用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')
于 2020-10-25T02:43:47.153 回答
0

假设您使用的是箱型的 windrose 模块。然后以下代码将您的风玫瑰转换为图像:

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)
于 2021-09-30T08:45:58.820 回答
-1

发生错误是因为您试图保存子图而不是图。尝试:

 fig,ax2 = plt.subplots(1,1) # Or whatever you need.
 # The windrose code you showed
 fig.savefig('./figures/windrose.png')
于 2019-01-07T18:09:07.753 回答