Python 在带有图例的网格上绘制多个圆

Python plot multiple circle on grid with legend

我想在 N×M 的网格上绘制,不同颜色和相同大小的圆圈。在 x,y 位置,可以是圆或什么都不是。

我想为每一列添加一个 x 标签(此处为一周),以及一个 ylabel(此处为主题)。

现在,我找到了一种使用子图绘制圆圈的方法,但我无法获得文本和网格。

这是我绘制圆圈的代码:

import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
from plotly.graph_objs import *
from matplotlib.gridspec import GridSpec

def dual_half_circle(center, radius, angle=0, ax=None, colors=('w','k'), **kwargs):
    """
    Add two half circles to the axes *ax* (or the current axes) with the 
    specified facecolors *colors* rotated at *angle* (in degrees).
    """
    if ax is None:
        ax = plt.gca()
    kwargs.update(transform=ax.transAxes, clip_on=False)
    theta1, theta2 = angle, angle + 180
    w1 = Wedge(center, radius, theta1, theta2, fc=colors[0], **kwargs)
    w2 = Wedge(center, radius, theta2, theta1, fc=colors[1], **kwargs)
    for wedge in [w1, w2]:
        ax.add_artist(wedge)
    return [w1, w2]

这是显示所有内容的代码(我删除了颜色条件):

DF = pd.DataFrame(np.random.choice([True, False], size = (15, 10)))

fig, ax = plt.subplots()
for ii in range(0,DF.shape[1]):    
    for jj in range(0,DF.shape[0]):
        if DF[ii][jj]:
            dual_half_circle((ii, -1*jj), radius=0.3, colors=('b','g'), angle=90, ax=ax)
            ax.axis('equal')

for ii in range(0,DF.shape[1]):       
    plt.annotate(xy= (ii, 1), s= 'W'+str(ii), fontsize = 100, verticalalignment='center', horizontalalignment='center')

for jj in range(0,DF.shape[0]):
    plt.annotate(xy =(-1, -1*jj),s= 'subj '+str(jj), fontsize = 100, verticalalignment='center', horizontalalignment='center')

plt.show()

这是结果,我没有注释也没有网格

Result

我是否应该从次要情节更改为经典情节以便能够添加我想要的内容? 谢谢您的帮助!

尝试以下操作。 (我还是不明白网格的东西,所以我把它漏掉了)

import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
import pandas as pd
import numpy as np


def dual_half_circle(center, radius, angle=0, ax=None, colors=('w','k'), **kwargs):
    """
    Add two half circles to the axes *ax* (or the current axes) with the 
    specified facecolors *colors* rotated at *angle* (in degrees).
    """
    if ax is None:
        ax = plt.gca()
    theta1, theta2 = angle, angle + 180
    w1 = Wedge(center, radius, theta1, theta2, fc=colors[0], **kwargs)
    w2 = Wedge(center, radius, theta2, theta1, fc=colors[1], **kwargs)
    for wedge in [w1, w2]:
        ax.add_artist(wedge)
    return [w1, w2]

DF = pd.DataFrame(np.random.choice([True, False], size = (15, 10)))

fig, ax = plt.subplots(figsize=(8,13))
for ii in range(0,DF.shape[1]):    
    for jj in range(0,DF.shape[0]):
        if DF[ii][jj]:
            dual_half_circle((ii, -1*jj), radius=0.3, colors=('b','g'), angle=90, ax=ax)
            ax.axis('equal')

for ii in range(0,DF.shape[1]):       
    plt.annotate(xy= (ii, 1), s= 'W'+str(ii), fontsize = 10, verticalalignment='center', horizontalalignment='center')

for jj in range(0,DF.shape[0]):
    plt.annotate(xy =(-1, -1*jj),s= 'subj '+str(jj), fontsize =10, verticalalignment='center', horizontalalignment='right')

ax.set_xlim(-1,10)
ax.set_ylim(-15,3)
plt.axis("off")
plt.plot([-1,10], [-15,3], alpha=0)
plt.tight_layout()
plt.show()