Python matplotlib 圆环图在一个楔形上具有较小的宽度

标签 python matplotlib donut-chart

我正在尝试重新创建一个圆环图,其中最后一个楔形比其他楔形更薄,如下所示:

enter image description here

但我找不到办法让最后一个楔子的宽度变小。所以我尝试让最后一个楔形与背景颜色相同,并在顶部画一个灰色圆圈。但是我无法在楔形下方绘制圆形,并使上面的图层在正确的位置透明。

有没有办法使单个楔子透明,或者以其他方式解决我的问题?

到目前为止,这是我的代码:

import matplotlib.pyplot as plt
donut = [150,10,20,20,30,40]
total = sum(donut)
grey_circle = plt.Circle((0,0),0.965,color='#CCCCCC', lw=1, fill=False)
centre_circle = plt.Circle((0,0),0.93,fc='white')
fig, ax = plt.subplots(figsize=(2, 2), subplot_kw=dict(aspect="equal"))
colors = ['#F8F5EB','#50E3C2','#FF9100','#002776','#C94096','#0071CD' ]
wedges, texts = ax.pie(donut, colors=colors, wedgeprops=dict(width=0.1), startangle=90)
fig.gca().add_artist(grey_circle)
fig.gca().add_artist(centre_circle)
fig.set_facecolor('#F8F5EB')
fig = plt.gcf()
plt.savefig('cirle.png',facecolor=fig.get_facecolor(), edgecolor='none', dpi=300)

plt.show() 

我的结果:

enter image description here

最佳答案

最简单的方法是绘制图表两次,一次绘制粗楔形,一次绘制细楔形。未绘制的楔形的颜色设置为“无”。设置明确的半径将为细楔创建正确的大小。

使用这种方法,不需要圆圈来隐藏东西。您仍然可以添加一个圆圈以在中心获得不同的颜色。只需添加 zorder=0确保圆圈在馅饼后面。

一些代码来说明这些概念:

import matplotlib.pyplot as plt

donut = [150, 10, 20, 20, 30, 40]

thin_indices = [0]  # indices of all wedges that should be thin
colors = ['#CCCCCC', '#50E3C2', '#FF9100', '#002776', '#C94096', '#0071CD']
colors_thin = [c if i in thin_indices else 'none' for i, c in enumerate(colors)]
colors_thick = [c if i not in thin_indices else 'none' for i, c in enumerate(colors)]
radius = 1
thick_width = 0.1
thin_width = 0.05
radius_thin = radius - 0.5 * (thick_width - thin_width)

fig, ax = plt.subplots(figsize=(2, 2), subplot_kw=dict(aspect="equal"))
ax.pie(donut, radius=radius, colors=colors_thick, wedgeprops=dict(width=thick_width), startangle=90)
ax.pie(donut, radius=radius_thin, colors=colors_thin, wedgeprops=dict(width=thin_width), startangle=90)
centre_circle = plt.Circle((0, 0), radius - thick_width/2, fc='white', zorder=0)
ax.add_artist(centre_circle)

fig.set_facecolor('#F8F5EB')
plt.savefig('cirle.png', facecolor=fig.get_facecolor(), edgecolor='none', dpi=300)

plt.show()

resulting donut plot

关于Python matplotlib 圆环图在一个楔形上具有较小的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59877425/

相关文章:

python - 对 matplotlib 桑基图进行动画处理

javascript - highcharts 圆环图中心文本与工具提示重叠

python - 我可以避免在Python中的2个for循环之间打印新行吗

python - Pandas 中复制与查看的终极 Elixir ?

python - 简单的肥尾对数分箱

python - 从 numpy ndarray 绘制 matplotlib

google-visualization - 如果只有一项 = 100%,则 Google donut chart 不会显示正文中的数字

ios - ShinobiCharts - donut 系列绘图区

python - 从 heroku 上的 rails 应用程序安装 pip 模块

python - python 中具有多个循环和 if 语句的列表理解