Skip to main content
 首页 » 编程设计

python之Windrose 图例括号格式和位置

2024年10月17日8jiqing9006

下面我使用基于 this 的 Windrose 绘制了风玫瑰图.首先,图例覆盖了玫瑰的一部分,但当我尝试使用 loc 设置其位置时,图例消失了。

其次,图例右括号是错误的,即 [0.0 : 1.0[ 知道我如何将其修复为 [0.0 : 1.0]

代码:

from windrose import WindroseAxes 
import matplotlib.cm as cm 
import matplotlib.pyplot as plt 
import numpy as np 
import pandas as pd 
 
df = pd.read_csv("C:\2007_GG_wind rose.csv") 
ws_SAR = df[' SARwind_10m'] 
wd_SAR = df['wind direction SAR model_int'] 
ws_mde = df['gg_mde'] 
wd_mde = df['wind direction MDE '] 
 
ax=WindroseAxes.from_ax() 
ax.bar(wd_SAR,ws_SAR,normed=True, opening=0.8, edgecolor='white') 
ax.set_legend() 
plt.title("SAR 10m U",y=1.08) #y=1.08 raises the title 

请您参考如下方法:

将原始 windrose.py 从 python 文件夹复制到所需的工作目录。以其他方式命名副本,例如windrose_edit.py。编辑文件并查找 get_labels() 函数。我是这样编辑的,但您可以根据自己的目的采用它。

def get_labels(): 
        labels = np.copy(self._info['bins']) 
        labels = ["%.1f : %0.1f" %(labels[i], labels[i+1]-0.1) \ 
                  for i in range(len(labels)-1)] 
        return labels 

您还可以增加下面几行图例的字体大小。

def set_legend(self): 
    l = self.legend(borderaxespad=-0.10) 
    plt.setp(l.get_texts(), fontsize=12) 

例如,最后导入您编辑的文件 将 windrose_edit 导入为 wind2

并将其与

一起使用
winddir_ = yourdata 
windspeed_ = yourdata 
 
fig = plt.figure(figsize=(12, 8), dpi=100, facecolor='w', edgecolor='w') 
rect = [0.1, 0.1, 0.8, 0.8] 
 
ax = wind2.WindroseAxes(fig, rect, facecolor='w') 
 
ax.contourf(winddir_, windspeed_, bins=6, normed=True, cmap=cm.RdYlBu_r) 
ax.set_legend() 

虽然是一个相对快速和懒惰的解决方法,但它并不漂亮