Rose Diagrams with Python

Wei Zhiyu
4 min readMar 8, 2022

--

Create a rose diagram with Python

Rose diagrams can show the circular distribution of directional data. They are commonly used to display the direction, strength and frequency of wind or ocean waves. There is a famous rose diagram called Florence Nightingale’s Rose Diagram, which was created by a nurse, statistician and reformer Florence Nightingale. It simply and clearly shows deaths distribution of epidemic disease.

In this blog, we will create rose diagrams with Python using two libraries, matplotlib and plotly.

Step 1: import libraries

First, let’s use matplotlib to plot.

import numpy as np
import matplotlib.pyplot as plt

Step 2: process data

Let’s create a random sample data and give them names.

n=12
x=[np.random.randint(1, 10) for i in range(n)]
names=['January','February','March','April','May','June','July','August','September','October','November','December']

Step 3: set angle for each bar

In polar coordinate system, a point is determined by a distance from a reference point and an angle from a reference direction. The distance is the value of data point and the angle needs to be set. Since there are n data points and maximum radian is 2*pi, the width of each bar is 2*pi/n, and the angle is accumulated sum.

rad = np.linspace(0, 2*np.pi, n, endpoint=False)
width=2*np.pi/n

Step 4: plot

Use bar plot and set polar as true. The first parameter is the angle of each bar, as calculated before, and the second parameter is distance, which is data value. Then set width and color, and a basic rose diagram is done.

plt.figure(figsize=(10, 10))
ax = plt.subplot(polar=True)
ax.bar(rad,x,width=width,color="pink",linewidth=1)

If you want to separate bars, add a parameter edgecolor and set it to white, then there is a white line which can separate bars.

ax.bar(rad,x,width=width,color="pink",edgecolor="white",linewidth=1)

If you want the center to be empty, add a parameter bottom and set a value to it. In my case, all bars will start on 2.

ax.bar(rad,x,width=width,color="pink",bottom=2,linewidth=1)

When there is one more dimension we want to visualize, such as different causes of deaths from Florence Nightingale’s Rose Diagram, we can add other bar plots and set different colors.

np.random.seed(1)
x2=[np.random.randint(1, 10)+x[i] for i in range(n)]
x3=[np.random.randint(1, 10)+x2[i] for i in range(n)]
plt.figure(figsize=(10, 10))
ax = plt.subplot(polar=True)
ax.bar(rad,x3,width=width,color="lightblue",edgecolor="white",linewidth=1)
ax.bar(rad,x2,width=width,color="grey",edgecolor="white",linewidth=1)
bars=ax.bar(rad,x,width=width,color="pink",edgecolor="white",linewidth=1)
plt.show()

Lastly let’s add labels to the diagram. Add value to the distance of data point to leave a little space between bar and label.

for i in range(n):
ax.text(rad[i],x3[i]+2,names[i])

There is another library plotly we can use to make rose diagrams. The plotly library can make more beautiful visualization and it is interactive web-based. If you are unable to find plotly library, just use pip install plotly to install it. Using bar_polar function, we don’t need to calculate angles by hand. Let’s use data wind from the library as an example.

import plotly.express as px# using the wind dataset
df = px.data.wind()
fig = px.bar_polar(df, r="strength", theta="direction",color="frequency",)
fig.show()

We can also use plotly.graph_objects to make rose diagrams.

import plotly.graph_objects as gofig = go.Figure()fig.add_trace(go.Barpolar(r=x2,name='data 1',))fig.add_trace(go.Barpolar(r=x3,name='data 2',))fig.show()

When to use a rose diagram

Rose diagrams work well when we want to visualize difference between groups. For example, in Florence Nightingale’s Rose Diagram, the blue, red and black areas show different degree of impact of deaths causes. Rose diagrams cannot clearly show the trend and distribution of data. Besides, when the number of data points is very large, it is not easy to see variation of bars, so rose diagrams are more proper for cyclic data such as week and month data.

Conclusion

In this blog, I showed how to make rose diagrams with Python using libraries matplotlib and plotly. You can use these methods to make rose diagrams to visualize frequency of values for monthly data or geography data. Try to build your rose diagram!

Reference

https://blog.csdn.net/weixin_43196531/article/details/104991624

https://www.geeksforgeeks.org/how-to-make-wind-rose-and-polar-bar-charts-in-plotly-python/

https://cookieblues.medium.com/how-to-make-a-nightingale-rose-diagram-only-using-matplotlib-c581441e8cc4

https://www.historyofinformation.com/detail.php?entryid=3815

--

--