pythonmatplotlibwindroserose-plot

Windrose legend bracket format and loc


Below I have plotted a wind rose using Windrose based on this. Firstly, the legend is covering part of the rose but when I try to use loc to set its location the legend disappears.

Secondly, the legend closing brackets are wrong i.e. [0.0 : 1.0[ any idea how I fix this to [0.0 : 1.0]

legend open bracket in place of close

code:

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

Solution

  • Copy the original windrose.py from your python folder to your desired working directory. Name the copy somehow else, e.g. windrose_edit.py. Edit the file and look for the get_labels() function. I edited like this, but you can adopt it to your purpose.

    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
    

    also you can increase the fontsize of the legend some lines below.

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

    Finally import your edited file for example import windrose_edit as wind2

    and use it with

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

    it's not beautiful although a relatively fast and lazy workaround