在嵌套饼图中创建不同大小的子部分。

huangapple go评论60阅读模式
英文:

How to make subsections of different sizes in a nested pie chart

问题

vals = np.array([[60., 32., 13., 5., 2.], [37., 40., 45., 3., 2.], [29., 10., 35., 2., 1.]])
英文:

I am working in python with matplotlib. I am trying to make a nested pie chart where the inner ring is a transportation mode and the outer is the subparts that it consists of.

I have this code:

import matplotlib.pyplot as plt
import numpy as np    

fig, ax = plt.subplots()

size = 0.3
vals = np.array([[60., 32., 13.], [37., 40., 45.], [29., 10., 35.]])

cmap = plt.get_cmap("tab20c")
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap([1, 2, 3, 5, 6, 7, 9, 10, 11])

ax.pie(vals.sum(axis=1), radius=3-0.9, colors=outer_colors,
       wedgeprops=dict(width=0.9, edgecolor='w'),labels=["Car","Public Transport","Bike"],
       pctdistance=1.1, labeldistance=0.65)

ax.pie(vals.flatten(), radius=3, colors=inner_colors,
       wedgeprops=dict(width=0.9, edgecolor='w'),labels=["Gasoline","Diesel","Electric","Bus","Tram","Train", "Normal Bike","E-Bike","Motorbike"],
       pctdistance=1.1, labeldistance=0.85)

ax.set(aspect="equal", title='')
plt.show()

Which leads to this nested pie chart

I can't figure out how to make the subparts different sizes. For example let's say I wanted to add Tricycle and Unicycle to the Bike category. So Car and Public Transport would be size 3, while Bike is size 5.

I can make all of the subparts be 2, 3 or 4 etc. as long as the lists in the vals array are of the same size. But if I make one of them a different size, then I'll end up with an AxisError. Is there any way to modify this code, so I can freely choose their sizes?

答案1

得分: 1

这是关于数据结构的内容。

将所有内容放入字典中使用户能够随意添加,而数字保持一致。

以下是使用您的数据的示例(为了清晰起见,我已删除了颜色映射):

import matplotlib.pyplot as plt

d = {
    "汽车": {"汽油": 60, "柴油": 32, "电动": 13},
    "公共交通": {"公交车": 37, "有轨电车": 40, "火车": 45},
    "自行车": {"普通自行车": 29, "电动自行车": 10, "摩托车": 35, "三轮车": 5, "独轮车": 15}
}

# 外部数值
汽车 = sum(d['汽车'].values())
公交 = sum(d['公共交通'].values())
自行车 = sum(d['自行车'].values())

外部数值 = [汽车, 公交, 自行车]
外部标签 = d.keys()

内部数值 = []
内部标签 = []
for 车辆类型 in d.values():
    for 车辆名称, 车辆数量 in 车辆类型.items():
        内部数值.append(车辆数量)
        内部标签.append(车辆名称)

fig, ax = plt.subplots()

ax.pie(外部数值, labels=外部标签, radius=1)
ax.pie(内部数值, labels=内部标签, radius=0.5)

ax.set_title("我的嵌套饼图")

plt.show()

这是结果图像的链接:

在嵌套饼图中创建不同大小的子部分。

英文:

This is about the structure of the data.

Putting everything into a dict gives the user the ability to add whatever they like and the numbers will be consistent.

Here is an example with your data (i have removed the colour map for clarity):

import matplotlib.pyplot as plt


d = {
    "car":{"Gasoline":60,"Diesel":32,"Electric":13},
    "Public Transport":{"Bus":37,"Tram":40,"Train":45}, 
    "bike":{"Normal Bike":29,"E-Bike":10,"Motorbike":35, "Tricycle":5, "Unicycle":15}
    }


# outer values
cars = sum(d['car'].values())
pt = sum(d['Public Transport'].values())
bike = sum(d['bike'].values())

outer_values = [cars, pt, bike]
outer_labels =d.keys()

inner_values = []
inner_labels = []
for vehicle_type in d.values():
    for vehicle_names, vehicle_amounts in vehicle_type.items():
        inner_values.append(vehicle_amounts)
        inner_labels.append(vehicle_names)


fig, ax = plt.subplots()

ax.pie(outer_values, labels=outer_labels, radius=1)
ax.pie(inner_values, labels=inner_labels, radius=0.5)

ax.set_title("My Nested Pie Chart")

plt.show()

And here is the result:

在嵌套饼图中创建不同大小的子部分。

huangapple
  • 本文由 发表于 2023年5月13日 19:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定