有没有办法在matplotlib热图中绘制饼图?

有没有办法在matplotlib热图中绘制饼图?,matplotlib,heatmap,pie-chart,Matplotlib,Heatmap,Pie Chart,我有一张有几行几列的热图。 以前,我为每个(行索引、列索引)绘制一个圆,并将此圆附加到圆列表中。我将circle_列表作为集合添加到轴 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=Non

我有一张有几行几列的热图。 以前,我为每个(行索引、列索引)绘制一个圆,并将此圆附加到圆列表中。我将circle_列表作为集合添加到轴

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()
但是,现在我需要绘制一个饼图,而不是一个圆。 饼图没有(行索引、列索引)参数

是否有方法在matplotlib热图的每个单元格中绘制饼图

使用圆圈更新热图中的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]
导致


您可以单独访问由
plt.pie
创建的每个楔块,然后使用
set\u radius
set\u 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]

也许你能适应?还可以看看@JohanC我需要网格线和适当的行和列标签。你能把那个楔子放到某个行索引和列索引中吗?我想你可以很容易地做一个映射,或者你可以对齐热图网格以匹配相应的楔子中心。特别是在您的情况下,您可以使用x和y记号位置作为楔子中心的位置。您可以为我提供的代码提供一个示例吗?刚刚编辑了我的ReplyTanks,我还以这种方式更新了我的代码。你可以看到新的数字。