【问题标题】:Is there a way to plot pie chart in matplotlib heatmap?有没有办法在 matplotlib 热图中绘制饼图?
【发布时间】:2021-03-18 12:13:55
【问题描述】:

我有一个包含多行和多列的热图。 以前,我为每个 (row_index,column_index) 绘制一个圆圈并将这个圆圈附加到一个 circle_list。我正在将 circle_list 作为集合添加到轴中。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import PatchCollection


def heatmap_with_circles(data_array,row_labels,column_labels,ax=None, cmap=None, norm=None, cbar_kw={}, cbarlabel="", **kwargs):

    circles=[]
    for row_index, row in enumerate(row_labels):
        for column_index, column in enumerate(column_labels):
            circles.append(plt.Circle((row_index,column_index),radius=0.4))

    col = PatchCollection(circles, array=data_array.flatten(), cmap=cmap, norm=norm)
    ax.add_collection(col)

    # We want to show all ticks...
    ax.set_xticks(np.arange(data_array.shape[1]))
    ax.set_yticks(np.arange(data_array.shape[0]))

    fontsize=10
    ax.set_xticklabels(column_labels, fontsize=fontsize)
    ax.set_yticklabels(row_labels, fontsize=fontsize)

    #X axis labels at top
    ax.tick_params(top=True, bottom=False,labeltop=True, labelbottom=False,pad=5)
    plt.setp(ax.get_xticklabels(), rotation=55, ha="left", rotation_mode="anchor")

    # We want to show all ticks...
    ax.set_xticks(np.arange(data_array.shape[1]+1)-.5, minor=True)
    ax.set_yticks(np.arange(data_array.shape[0]+1)-.5, minor=True)

    ax.grid(which="minor", color="black", linestyle='-', linewidth=3)
    ax.tick_params(which="minor", bottom=False, left=False)


data_array=np.random.rand(3,4)
row_labels=['Row1', 'Row2', 'Row3']
column_labels=['Column1', 'Column2', 'Column3','Column4']

fig, ax = plt.subplots(figsize=(1.9*len(row_labels),1.2*len(column_labels)))
ax.set_aspect(1.0)
ax.set_facecolor('white')
heatmap_with_circles(data_array,row_labels,column_labels, ax=ax)
plt.tight_layout()
plt.show()

但是,现在我需要绘制一个饼图而不是圆形。 而且饼图没有(row_index,column_index)参数。

有没有办法在 matplotlib 热图的每个单元格中绘制饼图?

更新heatmap_with_circles中的for循环如下:

for row_index, row in enumerate(row_labels,0):
    for column_index, column in enumerate(column_labels,0):
        wedges, _ = plt.pie([20, 10, 5])
        radius = 0.45
        [w.set_center((column_index,row_index)) for w in wedges]
        [w.set_radius(radius) for w in wedges]

结果

【问题讨论】:

标签: matplotlib heatmap pie-chart


【解决方案1】:

您可以单独访问由plt.pie 创建的每个楔形,然后使用set_radiusset_position 重新调整不同的楔形。

wedges, _ = plt.pie([1,2,3])
x_position, y_position = 0, 0
radius = 0.2
[w.set_center((x_position,y_position)) for w in wedges]
[w.set_radius(radius) for w in wedges]

编辑: 在您的代码中,在 for 循环中

    for row_index, row in enumerate(row_labels):
        for column_index, column in enumerate(column_labels):
            wedges, _ = plt.pie([1,2,3])
            [w.set_center((row_index,column_index)) for w in wedges]
            [w.set_radius(0.4) for w in wedges]

【讨论】:

  • 你能把那个楔子放到某个 row_index 和 column_index 上吗?
  • 我猜您可以轻松地进行映射,或者您可以对齐热图网格以匹配相应的楔形中心。特别是在您的情况下,您可以使用 x 和 y 刻度位置作为楔形中心的位置
  • 你能给我一个我提供的代码的例子吗?
  • 谢谢,我也以这种方式更新了我的代码。你可以看到新图。
  • 有没有办法为每个饼图楔形设置标签? [w.set_label('a') for w inwedges] 似乎不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-20
  • 2020-03-31
  • 1970-01-01
相关资源
最近更新 更多