您可以创建一个Waffle chart 并进行相同的计数。无论 Waffle 使用什么算法,都可能经过了很好的尝试。
完整的源代码在github,并且不是很长。采用我们得到的相同方法:
import matplotlib.pyplot as plt
from pywaffle import Waffle
num_leds = 24
values = [15812, 4162, 3054, 0, 43, 1564, 3080, 10, 320, 9]
labels = ['Black Coal', 'Brown Coal', 'Gas', 'Liquid Fuel', 'Other', 'Hydro', 'Wind', 'Large Solar', 'Small Solar', 'Battery']
colors = ['black', 'maroon', 'magenta', 'lightblue', 'red', 'mediumblue', 'coral', 'goldenrod', 'gold', 'blueviolet']
block_number_per_cat = [round(v * num_leds / sum(values)) for v in values]
blocks_per_label = {lab: num for lab, num in zip(labels, block_number_per_cat)}
print(blocks_per_label)
fig = plt.figure(
FigureClass=Waffle,
rows=1,
columns=num_leds,
values=values,
labels=labels,
colors=colors,
icons='lightbulb',
font_size=16,
legend={'loc': 'upper right', 'bbox_to_anchor': (1, -0.1)}
)
plt.show()
答案:
{'Black Coal': 14, 'Brown Coal': 4, 'Gas': 3, 'Liquid Fuel': 0, 'Other': 0, 'Hydro': 1, 'Wind': 3, 'Large Solar': 0, 'Small Solar': 0, 'Battery': 0}