How to divide wind Direction to angles to create wind-rose

Question:

here i divided wind direction(0 to 360 degrees) to 16 groups(‘N’,’NNE’,’NE’,’ENE’,’E’,’ESE’,’SE’,’SSE’,’S’,’SSW’,’SW’,’WSW’,’W’,’WNW’,’NW’,’NNW’). In my code N is -1 to 22.5 but in reality it should be between 348.75(360-11.25) and 11.25. To check my results I plotted windrose using Python Windrose package and python plotly px.bar_polar.

Windrose with windrose package (pip install windrose) :

ws=df.WS
wd=df.WD

ax = WindroseAxes.from_ax()
ax.bar(wd, ws, normed=True, opening=0.8, edgecolor='white')
ax.set_legend()

Output Windrose:

enter image description here

But When i plotted windrose using plotly(px.bar_polar) it gives **different windrose(**for same data):

# populate values in new columns
df['WindSpeedkm/h'] = df['WS'] * 3.6

bins = [-1, 10, 20, 30, 40,50, np.inf]
names = ['0-10 km/h', '10-20 km/h', '20-30 km/h', '30-40 km/h', '40-50 km/h','>50 km/h']
df['speed_kmhRange'] = pd.cut(df['WindSpeedkm/h'], bins, labels=names)


bins = [-1, 22.5,45,67.5,90,112.5,135,157.5,180,202.5,225,247.5,270,292.5,315,337.5,361]
names = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW']

Output Windrose:
enter image description here

I know this error is due to grouping the angles as i earlier mentioned, But how can i fix this? i need to use python plotly to the desired output.

DF:

{'WS': {0: 4.87,
  1: 4.87,
  2: 4.91,
  3: 4.97,
  4: 5.06,
  6: 0.21,
  7: 7.06,
  8: 6.62,
  10: 6.69,
  11: 7.0,
  12: 7.37,
  13: 7.41,
  14: 7.44,
  15: 7.41,
  16: 7.16,
  17: 6.69,
  18: 6.19,
  19: 5.91,
  20: 5.81,
  21: 5.75,
  22: 5.72,
  23: 5.72,
  24: 5.72,
  25: 0.37,
  26: 5.91,
  27: 6.09,
  28: 6.28,
  29: 6.28,
  30: 6.25,
  31: 6.03,
  32: 5.81,
  33: 5.59,
  34: 5.56,
  35: 5.34,
  36: 5.09,
  37: 4.87,
  38: 4.84,
  40: 4.84,
  41: 5.09,
  42: 5.69,
  43: 5.75,
  44: 5.78,
  45: 5.75,
  46: 5.5,
  47: 5.28,
  48: 5.09,
  49: 5.09,
  50: 5.12,
  51: 5.12,
  52: 5.12,
  53: 5.12,
  54: 5.22,
  55: 5.37,
  56: 5.56,
  57: 5.56,
  59: 5.53,
  60: 5.5,
  61: 5.53,
  62: 5.62,
  63: 5.69,
  64: 5.69,
  65: 5.62,
  66: 5.34,
  67: 5.09,
  68: 4.75,
  69: 4.69,
  70: 4.72,
  71: 4.75,
  72: 4.75,
  73: 0.0,
  74: 4.72,
  75: 4.72,
  76: 4.91,
  77: 5.16,
  78: 5.41,
  79: 5.44,
  80: 5.5,
  81: 5.53,
  82: 5.44,
  83: 5.34,
  84: 5.31,
  85: 5.25,
  86: 5.19,
  87: 5.12,
  89: 5.09,
  90: 5.06,
  91: 5.12,
  92: 5.41,
  93: 5.69,
  94: 6.0,
  95: 6.09,
  96: 6.12,
  97: 6.09,
  98: 6.09,
  99: 6.28,
  100: 6.5,
  101: 6.69,
  102: 6.5,
  104: 6.34,
  105: 6.47,
  106: 6.78,
  107: 7.03,
  108: 7.03,
  109: 7.0,
  111: 7.06,
  112: 7.03,
  113: 6.81,
  114: 6.56,
  115: 6.34,
  116: 6.28,
  117: 6.25,
  118: 6.25,
  120: 6.34,
  121: 6.41,
  122: 6.44,
  123: 6.44,
  124: 6.47,
  125: 6.81,
  126: 7.09,
  127: 7.16,
  128: 6.97,
  129: 6.75,
  130: 6.75,
  131: 7.0,
  132: 7.28,
  133: 7.69,
  135: 7.91,
  136: 8.06,
  137: 7.78,
  138: 6.78,
  139: 6.5,
  140: 6.22,
  141: 5.56,
  142: 4.91,
  143: 4.75,
  144: 5.03,
  145: 5.31,
  146: 5.37,
  147: 5.41,
  148: 5.44,
  149: 0.29,
  150: 5.72,
  151: 5.97,
  152: 6.28,
  153: 6.25,
  154: 6.22,
  155: 6.22,
  156: 6.25,
  157: 6.31,
  158: 6.34,
  159: 6.37,
  160: 6.16,
  161: 5.87,
  162: 5.62,
  163: 5.78,
  165: 5.94,
  166: 5.81,
  167: 5.53,
  168: 5.28,
  169: 5.22,
  170: 5.22,
  171: 5.22,
  172: 5.22,
  173: 5.28,
  174: 5.34,
  175: 5.44,
  176: 5.69,
  177: 5.91,
  178: 5.87,
  179: 0.29,
  180: 5.53,
  181: 5.56,
  182: 5.59,
  183: 5.62,
  184: 5.66,
  185: 5.69,
  186: 5.72,
  187: 5.75,
  188: 5.75,
  189: 5.78,
  190: 5.81,
  191: 5.84,
  192: 5.87,
  193: 6.0,
  194: 6.03,
  195: 6.03,
  196: 6.06,
  197: 6.12,
  198: 6.19,
  199: 6.41,
  200: 6.62,
  201: 6.81,
  202: 6.87,
  203: 6.91,
  204: 6.97,
  206: 7.22,
  207: 8.31,
  208: 8.69,
  209: 8.69,
  210: 8.25},
 'WD': {0: 358.56,
  1: 1.16,
  2: 2.87,
  3: 4.31,
  4: 5.55,
  6: 281.27,
  7: 1.02,
  8: 359.84,
  10: 357.78,
  11: 357.2,
  12: 356.2,
  13: 357.82,
  14: 1.04,
  15: 2.93,
  16: 3.83,
  17: 4.85,
  18: 6.42,
  19: 8.91,
  20: 10.37,
  21: 9.92,
  22: 8.24,
  23: 6.57,
  24: 6.36,
  25: 270.67,
  26: 8.14,
  27: 10.49,
  28: 11.55,
  29: 11.76,
  30: 13.27,
  31: 15.64,
  32: 17.75,
  33: 20.26,
  34: 22.77,
  35: 24.25,
  36: 23.99,
  37: 21.69,
  38: 19.18,
  40: 17.07,
  41: 16.1,
  42: 15.73,
  43: 14.93,
  44: 13.6,
  45: 13.07,
  46: 13.27,
  47: 14.32,
  48: 16.79,
  49: 19.34,
  50: 19.89,
  51: 17.7,
  52: 14.05,
  53: 11.39,
  54: 13.13,
  55: 15.91,
  56: 17.48,
  57: 17.82,
  59: 17.57,
  60: 17.23,
  61: 16.72,
  62: 14.69,
  63: 12.13,
  64: 10.01,
  65: 8.81,
  66: 7.51,
  67: 5.69,
  68: 4.01,
  69: 6.87,
  70: 10.57,
  71: 13.5,
  72: 14.15,
  73: 3.11,
  74: 13.04,
  75: 10.81,
  76: 7.27,
  77: 3.99,
  78: 1.13,
  79: 359.69,
  80: 359.27,
  81: 359.09,
  82: 1.77,
  83: 3.01,
  84: 2.77,
  85: 1.24,
  86: 359.84,
  87: 359.04,
  89: 358.02,
  90: 359.94,
  91: 358.87,
  92: 357.51,
  93: 357.85,
  94: 359.33,
  95: 0.54,
  96: 1.54,
  97: 2.19,
  98: 3.58,
  99: 5.73,
  100: 7.51,
  101: 8.63,
  102: 9.29,
  104: 10.89,
  105: 13.9,
  106: 18.03,
  107: 21.25,
  108: 22.94,
  109: 22.73,
  111: 16.53,
  112: 12.06,
  113: 8.73,
  114: 7.4,
  115: 8.46,
  116: 10.71,
  117: 12.32,
  118: 13.99,
  120: 16.41,
  121: 21.16,
  122: 26.01,
  123: 29.35,
  124: 29.24,
  125: 13.66,
  126: 7.49,
  127: 8.61,
  128: 9.95,
  129: 10.22,
  130: 8.68,
  131: 5.71,
  132: 2.72,
  133: 359.88,
  135: 357.9,
  136: 355.18,
  137: 353.98,
  138: 355.36,
  139: 355.09,
  140: 353.57,
  141: 352.17,
  142: 355.87,
  143: 357.23,
  144: 359.79,
  145: 359.94,
  146: 358.07,
  147: 358.04,
  148: 1.58,
  149: 279.41,
  150: 4.73,
  151: 7.84,
  152: 10.57,
  153: 10.43,
  154: 9.5,
  155: 7.4,
  156: 5.26,
  157: 3.31,
  158: 1.75,
  159: 358.62,
  160: 350.52,
  161: 345.59,
  162: 341.33,
  163: 350.82,
  165: 359.53,
  166: 3.81,
  167: 2.9,
  168: 3.86,
  169: 13.1,
  170: 23.17,
  171: 31.06,
  172: 30,
  173: 25.04,
  174: 19.94,
  175: 15.79,
  176: 14.27,
  177: 13.06,
  178: 11.6,
  179: 279.44,
  180: 9.74,
  181: 10.06,
  182: 10.05,
  183: 8.97,
  184: 6.87,
  185: 4.44,
  186: 2.6,
  187: 1.47,
  188: 1.27,
  189: 2.29,
  190: 3.58,
  191: 3.14,
  192: 0.98,
  193: 359.71,
  194: 2.94,
  195: 6.95,
  196: 11.27,
  197: 14.19,
  198: 15.72,
  199: 15.46,
  200: 14.33,
  201: 12.98,
  202: 11.27,
  203: 9.73,
  204: 8.73,
  206: 8.59,
  207: 8.51,
  208: 7.7,
  209: 7.22,
  210: 7.27}}
Asked By: johnson

||

Answers:

If I have understood it correctly, your problem is about creating 16 bins with one bin ranging from 348.75 to 11.25.

To solve the problem you can create 17 bins. The first bin ranges from 0 to 11.25, the last one from 348.75 to 360.

As pd.cut() requires that each bin has an unique label, you must name the last bin different than the first one despite they represent the same direction, i.e. label them N and N2.
In an additional step after binning, you can then rename N2 to N.

bins = np.linspace(0, 360, 17)+11.25
bins = np.insert(bins, 0, 0)
names = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW', 'N2']
df['wind_dir_Range'] = pd.cut(df['WD'], bins, labels=names)
df['wind_dir_Range'] = df['wind_dir_Range'].replace('N2', 'N')
Answered By: Pascalco
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.