python - 使用 pywaffle 在 python 中创建带有阴影的华夫饼图
问题描述
我想为以下数据框创建一个灰色形状的华夫饼图
data = pd.DataFrame({'Category': ['a', 'b', 'c', 'd'], 'no_occurrence' : [594, 5, 10, 9]})
这是我到目前为止根据这篇文章所做的
import matplotlib.pyplot as plt
from pywaffle import Waffle
fig = plt.figure(
FigureClass=Waffle,
rows=5,
colors = ('lightgrey', 'black', 'darkgrey', 'lightgrey'),
values=list(data['no_occurrence']/4),
labels=list(data['Category']),
icons = 'sticky-note',
icon_size = 11,
figsize=(12, 8),
icon_legend = True,
legend={'loc': 'lower left','bbox_to_anchor': (0, -0.4), 'ncol': len(data), 'fontsize': 8}
)
由于灰色的形状很难区分,我想孵化最后一个类别(在图中和图例中),但我不知道如何在颜色中添加阴影。我有一个具有相同类别的条形图,我在其中添加了阴影,所以我想保持一致。
解决方案
当使用图标作为华夫饼图中的元素时,它们在内部被表示为具有特殊字体的文本对象。
使用patheffects
,可以将阴影添加到图例中的图标中。
由于您没有提供玩具数据,也没有提供图像,因此我制作了一些数据来展示这些想法。由于我的图例图标比图中的图标小,因此我为图例使用了更密集的阴影线。
import matplotlib.pyplot as plt
from matplotlib import patheffects
import numpy as np
from pywaffle import Waffle
values = [5, 14, 17, 18]
fig = plt.figure(
FigureClass=Waffle,
rows=5,
colors=('lightgrey', 'black', 'darkgrey', 'lightgrey'),
values=values,
labels=[*'abcd'],
icons='sticky-note',
icon_size=60,
figsize=(12, 8),
icon_legend=True,
legend={'loc': 'lower left', 'bbox_to_anchor': (0, -0.4), 'ncol': 4, 'fontsize': 15,
'facecolor': 'white', 'edgecolor': 'black'})
for t in fig.ax.texts[-values[-1]:]:
t.set_path_effects([patheffects.PathPatchEffect(hatch='xxx', fc='lightgrey', ec='white')]) # color='lightgrey')])
fig.ax.legend_.legendHandles[-1].set_path_effects(
[patheffects.PathPatchEffect(hatch='xxxxx', fc='lightgrey', ec='white')])
fig.tight_layout()
plt.show()
推荐阅读
- botframework - 无法连接到远程机器人技能?
- xpath - Mule 3.9 - 无法使用 Xpath3 NODESET 的 foreach 获取 xml 值
- docker - 有没有办法将 nexus 配置为 gke 集群的私有注册表
- excel - Power Automate,将 Sharepoint Excel 文件导出到本地
- reactjs - 为什么映射项状态在 React 中会相互影响?
- python - Django后向外键查询不起作用
- android - Google Material You:我们如何使用与system+Google Apps相同的Color Accent?
- python - 迭代 NumPy 数组以获取时间步长
- floating-point - 尝试表示 2^(-23) 时,难以掌握单精度 IEEE-754 中的“转换错误”
- python - 如何将多个文件复制到另一个目录中?