Matplotlib 是否有一种方法可以标记此网格中饼图的每个楔形部分?

Matplotlib 是否有一种方法可以标记此网格中饼图的每个楔形部分?,matplotlib,label,pie-chart,Matplotlib,Label,Pie Chart,我想在一个网格中有多个饼图 每个饼图将具有不同数量的楔块、值和标签 下面的代码在一个饼图中显示多个标签 有没有办法在这个网格中标记每个楔形饼图 import matplotlib.pyplot as plt import numpy as np def heatmap_with_circles(data_array,row_labels,column_labels,ax=None, cmap=None, norm=None, cbar_kw={}, cbarlabel=""

我想在一个网格中有多个饼图

每个饼图将具有不同数量的楔块、值和标签

下面的代码在一个饼图中显示多个标签

有没有办法在这个网格中标记每个楔形饼图

import matplotlib.pyplot as plt
import numpy as np

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

    for row_index, row in enumerate(row_labels,0):
        for column_index, column in enumerate(column_labels,0):
            print('row_index: %d column_index: %d' %(row_index,column_index))
            if row_index==0 and column_index==0:
                colors=['indianred','orange','gray']
                values=[10,20,30]
            else:
                values=[45,20,38]
                colors=['pink','violet','green']

            wedges, text = plt.pie(values,labels=['0', '2', '3'],labeldistance = 0.25,colors=colors)
            print('len(wedges):%d wedges: %s, text: %s' %(len(wedges), wedges, text))
            radius = 0.45
            [w.set_center((column_index,row_index)) for w in wedges]
            [w.set_radius(radius) for w in wedges]

    # 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=2)
    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(column_labels),1.2*len(row_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()

更新带有圆圈的热图后

def heatmap_with_circles(data_array,row_labels,column_labels,ax=None, cmap=None, norm=None, cbar_kw={}, cbarlabel="", **kwargs):
    labels = ['x', 'y', 'z']

    for row_index, row in enumerate(row_labels,0):
        for column_index, column in enumerate(column_labels,0):
            print('row_index: %d column_index: %d' %(row_index,column_index))
            if row_index==0 and column_index==0:
                colors=['indianred','orange','gray']
                values=[10,20,30]
            else:
                values=[45,20,38]
                colors=['pink','violet','green']

            # wedges, texts = plt.pie(values,labels=['0', '2', '3'],labeldistance = 0.45,colors=colors)
            wedges, texts = plt.pie(values,labeldistance = 0.25,colors=colors)
            print('text:%s len(wedges):%d wedges: %s' %(texts, len(wedges), wedges))
            radius = 0.45
            [w.set_center((column_index,row_index)) for w in wedges]
            [w.set_radius(radius) for w in wedges]
            [text.set_position((text.get_position()[0]+column_index,text.get_position()[1]+row_index)) for text in texts]
            [text.set_text(labels[text_index]) for text_index, text in enumerate(texts,0)]


我得到了下图:)

您可以循环遍历每个饼图的文本,获取其xy位置,添加列索引和行索引,并将其设置为新位置

对现有代码的一些小更改:

  • ax.grid(which=“minor”,…,clip\u on=False)
    确保粗线完全显示,也在边框附近
  • ax.set_xlim(xmin=-0.5)
    设置限制
导入matplotlib.pyplot作为plt
将numpy作为np导入
带圆圈的def热图(数据数组、行标签、列标签,ax=None):
ax=ax或plt.gca()
对于行索引,枚举中的行(行标签,0):
对于列索引,枚举中的列(列标签,0):
颜色=np.random.choice(['indianred','orange','gray','pink','violet','green'],3,replace=False)
值=np.random.randint(10,41,3)
楔形,text=plt.pie(值,标签=['1','2','3'],标签距离=0.25,颜色=colors)
半径=0.45
对于w英寸楔块:
w、 设置中心((列索引、行索引))
w、 设置半径(半径)
w、 设置颜色(“白色”)
#w.set_线宽(1)
对于文本中的t:
x、 y=t.获得位置()
t、 设置位置((x+列索引,y+行索引))
#我们想显示所有的滴答声。。。
ax.set\u xticks(np.arange(data\u array.shape[1]))
ax.set\u yticks(np.arange(data\u array.shape[0]))
字体大小=10
ax.set_xticklabels(列标签,fontsize=fontsize)
ax.设置字体标签(行字体标签,字体大小=字体大小)
#顶部的X轴标签
ax.tick_参数(top=True,bottom=False,labeltop=True,labelbottom=False,pad=5)
plt.setp(ax.get\xticklabels(),rotation=55,ha=“left”,rotation\u mode=“anchor”)
#我们要显示所有小刻度。。。
ax.set\u xticks(np.arange(data\u array.shape[1]+1)-.5,minor=True)
ax.set\u yticks(np.arange(数据数组形状[0]+1)-.5,次方=真)
ax.set_xlim(xmin=-.5)
ax.set_ylim(ymin=-.5)
ax.grid(which=“minor”,color=“black”,linestyle='-',linewidth=2,clip_on=False)
ax.勾号参数(axis=“both”,which=“both”,length=0)#隐藏勾号
数据数组=np.random.rand(3,4)
行_标签=['Row1'、'Row2'、'Row3']
列标签=['Column1'、'Column2'、'Column3'、'Column4']
图,ax=plt.子批次(图尺寸=(1.9*len(列标签),1.2*len(行标签)))
ax.set_纵横比(1.0)
ax.set_facecolor(“白色”)
带圆圈的热图(数据数组、行标签、列标签,ax=ax)
plt.紧_布局()
plt.show()

您是如何做到这一点的?此外,我的代码必须为每个饼图编写标签,但不知何故,只有它们才写在第一个饼图上。看起来比我的要好得多。完美:)您可以添加
ax.tick_params(axis=“both”,length=0)
隐藏勾号感谢您的帮助,现在它看起来很完美。顺便问一下,是什么在饼图的各个部分之间创建了这条白线?在我的例子中,没有这样的白线。您可以更改楔块的边缘颜色:
w.set\u edgecolor('white')