导出Python-Windrose图作为EPS

时间:2019-01-07 17:59:52

标签: python plot save

我用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'

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

谢谢!

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我们可以使用pyplot.savefig()中的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')