I am trying to recreate this: enter image description here

我已经拥有了大部分:

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')

Which produces this: enter image description here

and with the axes on: enter image description here

我的问题是:

有没有办法重叠轴线,这样我就能让它们靠得更近?

I can not do #ax.set_thetamax(180) because it will cut the rounded edges at the beginning: enter image description here

我也try 过TARTH_Layout,它适用于笛卡儿轴,但不适用于极轴和 Plt.subplots_adjust(wspace=0,hspace=0)不执行任何操作

prize 问题: 如何在不对偏移量进行硬编码的情况下定位径向标签和数据标签(分别为国家/地区名称)?

推荐答案

您可以按如下方式调整距ionic 图分隔(另请参见here):

    ...

    shift_axes = 0.18 if direction == 1 else -0.21

    box = ax.get_position()
    box.x0 = box.x0 + shift_axes
    box.x1 = box.x1 + shift_axes
    ax.set_position(box)

Python相关问答推荐

提取两行之间的标题的常规表达

Python daskValue错误:无法识别的区块管理器dask -必须是以下之一:[]

如何比较numPy数组中的两个图像以获取它们不同的像素

处理带有间隙(空)的duckDB上的重复副本并有效填充它们

如何在polars(pythonapi)中解构嵌套 struct ?

如何在虚拟Python环境中运行Python程序?

如何使用Pandas DataFrame按日期和项目汇总计数作为列标题

Polars asof在下一个可用日期加入

启动带有参数的Python NTFS会导致文件路径混乱

在Python中调用变量(特别是Tkinter)

Geopandas未返回正确的缓冲区(单位:米)

找到相对于列表索引的当前最大值列表""

Python避免mypy在相互引用中从另一个类重定义类时失败

在numpy数组中寻找楼梯状 struct

BeautifulSoup-Screper有时运行得很好,很健壮--但有时它失败了::可能这里需要一些更多的异常处理?

如何根据一定条件生成段id

遍历列表列表,然后创建数据帧

将相应的值从第2列合并到第1列(Pandas )

有没有一种方法可以根据不同索引集的数组从2D数组的对称子矩阵高效地构造3D数组?

Sknowled线性回归()不需要迭代和学习率作为参数