Skip to content

Instantly share code, notes, and snippets.

@tomowarkar
Last active February 12, 2023 08:07
Show Gist options
  • Save tomowarkar/610e1671418b7eacf7b8178b7ed5102c to your computer and use it in GitHub Desktop.
Save tomowarkar/610e1671418b7eacf7b8178b7ed5102c to your computer and use it in GitHub Desktop.

output.png

plot

df.head()

theta r
0 3.4483 1.17782
1 4.49367 0.770008
2 3.78727 1.23519
3 3.4236 1.46219
4 2.6619 0.748753
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(0)
N = 100
df = pd.DataFrame({
'theta': np.random.rand(N) * 2 * np.pi,
'r': np.random.rand(N)+.5
})
def style(ax):
ax.set_theta_offset(np.pi / 2)
ax.set_thetalim(0, 2 * np.pi)
ax.set_rlabel_position(np.pi / 2)
ax.set_theta_direction(-1)
ax.tick_params("both", grid_alpha=0.50, grid_zorder=-10, grid_linewidth=0.5)
return ax
fig = plt.figure(figsize=(18, 6))
ax = style(plt.subplot(1, 4, 1, projection="polar"))
ax.scatter(df['theta'], df['r'])
ax = style(plt.subplot(1, 4, 2, projection="polar"))
data = df.sort_values('theta')
data = pd.concat([data, data.iloc[0:1]])
ax.plot(data['theta'], data['r'])
ax = style(plt.subplot(1, 4, 3, projection="polar"))
bmw = 20
data = (np.degrees(df['theta']).round() + bmw//2) % 360
data = np.radians(data//bmw*bmw).value_counts().reset_index().sort_values('index')
data = pd.concat([data, data.iloc[0:1]])
ax.fill(data['index'], data['theta'], color="C1", alpha=0.25)
ax.plot(data['index'], data['theta'], color="C1")
ax.set_xticks(np.radians(np.arange(0, 360, bmw)))
ax = style(plt.subplot(1, 4, 4, projection="polar"))
bmw = 20
data = pd.DataFrame(df['theta'].apply(np.degrees).apply(np.floor))
data = np.radians(data//bmw*bmw).value_counts().reset_index().rename(columns={0:'count'}).sort_values('theta')
data = pd.merge(pd.DataFrame({'theta': np.radians(range(0, 360, bmw))}), data, on='theta', how='left').fillna(0)
T = pd.concat([data['theta'], data['theta'].shift(-1)], axis=1).values.reshape(-1)
T[-1] = T[0]
R = pd.concat([data['count'], data['count']], axis=1).values.reshape(-1)
T = np.append(T, T[0])
R = np.append(R, R[0])
ax.fill(T, R, color="C1", alpha=0.25)
ax.plot(T, R, color="C1")
ax.set_xticks(np.radians(np.arange(0, 360, bmw)))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment