Python使用图例在网格上绘制多个圆圈

时间:2017-03-10 14:54:38

标签: python matplotlib annotations grid subplot

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

我想为每一列(这将是一周)和一个ylabel(这将是一个主题)有一个x标签。

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

以下是我绘制圆圈的代码:

#include <iostream>
#include <string>
#include <list>
#include <algorithm>

struct Object {
    int id;
    string name;
};

int main()
{
  list<Object> objectList {{1, "one_1"}, {2, "two_1"}, {3, "three_1"}, {2, "two_2"}, {1, "one_2"}, {4, "four_1"}, {3, "Three_2"}, {4, "four_2"}};
  list<int> idList {3, 2, 4, 1};

  objectList.sort([&idList] (const Object& o1, const Object& o2) -> bool
    { return std::find(++std::find(idList.begin(), idList.end(), o1.id), idList.end(), o2.id) != idList.end(); });

  for(const auto& o: objectList) cout << o.id << " " << o.name << "\n";
}

/* OUTPUT: 
3 three_1
3 Three_2
2 two_1
2 two_2
4 four_1
4 four_2
1 one_1
1 one_2
*/

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

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]

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

Result

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

1 个答案:

答案 0 :(得分:0)

尝试以下方法。 (我仍然不理解网格的东西,所以我把它留了出来)

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()

enter image description here