我已经拥有了大部分:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
color_dict = {"Denmark": "#A54836", "Norway": "#2B314D", "Sweden": "#5375D4"}
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['sub_total'] = df.groupby('year')['sites'].transform('sum')
df = df.sort_values(['countries', 'sites'], ascending=True ).reset_index(drop=True)
fig, axes = plt.subplots(ncols=2, figsize=(10,5), facecolor = "#FFFFFF", subplot_kw=dict(polar=True) )
fig.tight_layout(h_pad=-40)
countries = df.countries.unique()
colors = color_dict.keys()
years = df.year.unique()
offsets=[0.3,0.2,0.15]
directions = [1,-1]
ylabel = [0.58, 0.68, 0.78]
for ax,year, direction in zip(axes.ravel(),years, directions):
temp_df = df[df.year==year]
for i, (country,site, color,offset,yl) in enumerate(zip(temp_df.countries, temp_df.sites, colors, offsets, ylabel)):
angle_range = np.linspace(0, site*7)
theta =[np.deg2rad(a) for a in angle_range]
r = np.full(len(angle_range), i + 1) # identical radius values to draw an arc
print(theta,r)
ax.plot(theta,
r,
linewidth=15,
solid_capstyle="round",
color=color_dict[color])
ax.text(0.49,yl, country,transform=plt.gcf().transFigure, ha = "center")
ax.annotate(site, xy= ( theta[-1],r[-1]), color="w",ha="center" ,va="center")
# increase the r limit, making a bit more space to show thick arcs correctly
ax.set_rmax(4)
ax.set_theta_zero_location('N')
ax.set_theta_direction(direction)
ax.grid(False)
#ax.set_thetamax(180)
ax.axis('off')
我的问题是:
有没有办法重叠轴线,这样我就能让它们靠得更近?
I can not do #ax.set_thetamax(180) because it will cut the rounded edges at the beginning:
我也try 过TARTH_Layout,它适用于笛卡儿轴,但不适用于极轴和 Plt.subplots_adjust(wspace=0,hspace=0)不执行任何操作
prize 问题: 如何在不对偏移量进行硬编码的情况下定位径向标签和数据标签(分别为国家/地区名称)?