Skip to content

Instantly share code, notes, and snippets.

@SebastianoF
Last active April 24, 2022 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SebastianoF/ba0a130f3c72e2be90e44f8b0c89955d to your computer and use it in GitHub Desktop.
Save SebastianoF/ba0a130f3c72e2be90e44f8b0c89955d to your computer and use it in GitHub Desktop.
b001_13.py
import seaborn as sns
sns.set_style("darkgrid", {"grid.color": ".6", "grid.linestyle": ":"})
def radar_histogram(ax, df):
"""
Input:
df with at least 2 columns distance_km and bearing_deg.
Output: radar histogram plot.
"""
# Figures parameter
directions = 40
bottom = 4
height_scale = 8
# bearing: degrees from nort pole clockwise
bearing_bins = np.linspace(0, 360, directions+1, endpoint=False)
# angle visualisation: rad from east counterclockwise
theta = - 1 * np.linspace(0, 2 * np.pi, directions, endpoint=False) + np.pi/2
width = (2*np.pi) / directions
# data binning
se_bins = pd.cut(df["bearing_deg"].to_numpy(), bearing_bins)
np_bins = se_bins.value_counts().to_numpy()
bins = height_scale * np.array(np_bins) / np.max(np_bins)
# Uncomment to debug figure:
# bins = range(directions)
# plotting
ax_bars = ax.bar(theta, bins, width=width, bottom=bottom, color="blue")
ax.set_yticklabels([])
ax.set_xticks(np.linspace(0, 2 * np.pi, 8, endpoint=False))
ax.set_xticklabels(['E', '', 'N', '', 'W', '', 'S', ''])
ax.grid(False)
return ax
def radar_histogram_3_levels(ax, df):
"""
Input:
df with at least 2 columns distance_km and bearing_deg.
Output: radar histogram plot.
"""
# Figures parameter
directions = 40
height_scale = 2
bottom_inner = 2
bottom_betw = 5
bottom_outer = 8
# bearing: degrees from nort pole clockwise
bearing_bins = np.linspace(0, 360, directions+1, endpoint=False)
# angle visualisation: rad from east counterclockwise
theta = - 1 * np.linspace(0, 2 * np.pi, directions, endpoint=False) + np.pi/2
width = (2*np.pi) / directions
# data binning
df_inner = df[df["distance_km"] <= 10]
se_bins_inner = pd.cut(df_inner["bearing_deg"].to_numpy(), bearing_bins)
np_bins_inner = se_bins_inner.value_counts().to_numpy()
bins_inner = height_scale * np.array(np_bins_inner) / np.max(np_bins_inner)
df_betw = df[(df["distance_km"] > 10) & (df["distance_km"] <= 20)]
se_bins_betw = pd.cut(df_betw["bearing_deg"].to_numpy(), bearing_bins)
np_bins_betw = se_bins_betw.value_counts().to_numpy()
bins_betw = height_scale * np.array(np_bins_betw) / np.max(np_bins_betw)
df_outer = df[df["distance_km"] > 20]
se_bins_outer = pd.cut(df_outer["bearing_deg"].to_numpy(), bearing_bins)
np_bins_outer = se_bins_outer.value_counts().to_numpy()
bins_outer = height_scale * np.array(np_bins_outer) / np.max(np_bins_outer)
# plotting
ax_bars_inner = ax.bar(theta, bins_inner, width=width, bottom=bottom_inner, color="blue")
ax_bars_betw = ax.bar(theta, bins_betw, width=width, bottom=bottom_betw, color="blue")
ax_bars_outer = ax.bar(theta, bins_outer, width=width, bottom=bottom_outer, color="blue")
ax.set_yticklabels([])
# uncomment to add values on radius axis
# ax.set_yticks(np.arange(0,10,1.0))
# ax.set_yticklabels(['', '', '', '<=10Km ', '', '', '>10Km\n <=20Km', '', '', '>20Km'])
ax.set_xticks(np.linspace(0, 2 * np.pi, 8, endpoint=False))
ax.set_xticklabels(['E', '', 'N', '', 'W', '', 'S', ''])
ax.grid(False)
return ax
fig = plt.figure(figsize=(12, 12))
ax11 = fig.add_subplot(221, polar=True)
ax11 = radar_histogram(ax11, df_commuters_st_luke_office)
ax11.set_title("Saint Luke Office", y=1.08, x=1.1)
ax12 = fig.add_subplot(222, polar=True)
ax12 = radar_histogram_3_levels(ax12, df_commuters_st_luke_office)
ax21 = fig.add_subplot(223, polar=True)
ax21 = radar_histogram(ax21, df_commuters_albert_road)
ax21.set_title("Albert Road Office", y=1.08, x=1.1)
ax22 = fig.add_subplot(224, polar=True)
ax22 = radar_histogram_3_levels(ax22, df_commuters_albert_road)
plt.plot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment