Adding tick marks and labels on all thetagridlines on a polar chart

14 Views Asked by At

I am trying to recreate this:

enter image description here

and got this far ( I didnt plot the flags so you can reproduce the code):

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.interpolate import interp1d


data = {
    "year": [2004, 2022, 2004, 2022, 2004, 2022],
    "countries" : ["Sweden", "Sweden", "Denmark", "Denmark", "Norway", "Norway"],
    "sites": [13,15,4,10,5,8]
}

df= pd.DataFrame(data)
df = df.sort_values(['year', 'countries'], ascending=True ).reset_index(drop=True)
fig, ax= plt.subplots(figsize=(10,5), facecolor = "#FFFFFF",sharex=True, sharey=True, subplot_kw=dict(polar=True) )

countries = df.countries.unique()
sites =  np.array(df.groupby(['year'], sort=False).sites.apply(list).tolist())
sites_radar = np.concatenate([sites,sites[:, :1]], axis = 1)
angles = np.linspace(0, 2*np.pi, len(countries), endpoint=False)
years = df.year.unique()
colors = ["#CC5A43","#2C324F",]


for  site_radar,color in zip(sites_radar,colors):
    angles = np.linspace(0, 2*np.pi, len(countries), endpoint=False)
    angles = np.concatenate((angles,[angles[0]]))    
    ax.plot(angles, site_radar, 'o', linewidth=2, color = color)
    print(angles, sites_radar)

    for angle in angles:
        
        ax.set_rgrids(range(0,16,1), angle =angle)



# Radial tick parameters
radial_ticks = np.arange(0,20,5)
ax.set_yticks(radial_ticks)
ax.set_yticklabels(radial_ticks)

# draw a line on the y axis at each label
ax.set_yticks(np.arange(1, 16, 1))
ax.tick_params(axis='y',  which="major",  pad = 20, left=True, color="#D7DCDE",labelsize=10,labelcolor="#C8CCCF",  length=6, width=1, direction='inout')

for label in ax.get_xticklabels():
    label.set_horizontalalignment('right')

ax.set_xticks(angles)
ax.xaxis.set_ticks(angles[:-1], labels = countries)

ax.set_theta_zero_location('N')
ax.spines["polar"].set_color("none")
ax.yaxis.grid(False)

Which produces:

enter image description here

How can I add the ticks on all y/theta axis and move the labels to be centered with the ticks?

I have found this answer, but because it is written as an object, I am not able to decipher what is going on :(

0

There are 0 best solutions below