Matlab 用方框显示数据并向方框中添加圆圈/记号

Matlab 用方框显示数据并向方框中添加圆圈/记号,matlab,matplotlib,matlab-figure,Matlab,Matplotlib,Matlab Figure,我有一个看似简单的任务,如果有人能帮我一把或给我指出正确的方向,我将非常感激 我有一个由1和0组成的向量,我想用下面的方式创建一个数据图:我想要一个由两个正方形组成的垂直块,对于向量中的每个元素,我想在每个正方形中添加一个计数(可以是一个圆,一个点,任何东西)。因此,对于eg,顶部的正方形可以计算所有的0,底部的正方形可以计算所有的1 例如,如果我有一个50个0和50个1的向量,在运行这个文件的末尾,每个块将包含50个圆圈/计数标记。我首先创建矩形,如下所示: axis([0 1 0 1]) r

我有一个看似简单的任务,如果有人能帮我一把或给我指出正确的方向,我将非常感激

我有一个由1和0组成的向量,我想用下面的方式创建一个数据图:我想要一个由两个正方形组成的垂直块,对于向量中的每个元素,我想在每个正方形中添加一个计数(可以是一个圆,一个点,任何东西)。因此,对于eg,顶部的正方形可以计算所有的0,底部的正方形可以计算所有的1

例如,如果我有一个50个0和50个1的向量,在运行这个文件的末尾,每个块将包含50个圆圈/计数标记。我首先创建矩形,如下所示:

axis([0 1 0 1])
rectangle('Position',[0.1,0.3,0.2,0.2])
rectangle('Position',[0.1,0.8,0.2,0.2])
但是,当我想根据向量的元素是零还是一,将计数标记添加到每个圆上时,我被卡住了

非常感谢任何帮助

可以使用命令绘制圆(或正方形)。为了避免在同一点上绘制所有标记,可以考虑在位置上添加一些随机性。 正方形的中心

c0 = [0.2, 0.4];
c1 = [0.2, 0.9];
假设你有以下向量

v = [0 0 0 1 1 1 0 1 0 0 1 1 0 0 0 0];
有点随意

r = 0.025*randn( numel( v ), 2 ); % you may control the "spread" of the markers using the scaling factor here

% do the plot
axis([0 1 0 1]); hold on;
rectangle('Position',[0.1,0.3,0.2,0.2])
rectangle('Position',[0.1,0.8,0.2,0.2])     
s0 = v == 0;
scatter( c0(1)+r(s0,1), c0(2)+r(s0,2), 20, 'ob' ); % blue circles
scatter( c1(1)+r(~s0,1), c1(2)+r(~s0,2), 20, 'ob' ); % blue circles

基本的问题似乎是如何以一种美观的方式在每个盒子中分配计数、圆圈等<在某种程度上,Shai的答案是好的。如果你接受这个答案,我建议你使用半透明的符号。这样,重叠符号将可见

让我们用
matplotlib
试试这个:

import matplotlib.pyplot as plt
import numpy as np

# input vector with 100 elements:
v = np.random.randint(0,2,100)

# calculate the number of 0's and 1's:
n_zeros = np.sum(v == 0)
n_ones = len(v) - n_zeros

# create the plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(0,2)
ax.set_ylim(0,4)
ax.add_patch(plt.Rectangle((.5, .5), 1, 1, facecolor='none', edgecolor='k'))
ax.add_patch(plt.Rectangle((.5, 2.5), 1, 1, facecolor='none', edgecolor='k'))

# calculate some random coordinates for zeros and ones
c_zeros = np.array([.6, .6]) + .8 * np.random.random((n_zeros, 2))
c_ones = np.array([.6, 2.6]) + .8 * np.random.random((n_ones, 2))

# plot ones with semi-transparent red
ax.plot(c_ones[:,0], c_ones[:,1], 'o', mfc=(1.,0.,0.,.5), mec='none', markersize=10)
# plot zeros with semi-transparent blue
ax.plot(c_zeros[:,0], c_zeros[:,1], 'o', mfc=(0., 0., 1., .5), mec='none', markersize=10)
我们得到:

该图有两个明显的缺点:

  • 随机性不会给出均匀分布
  • 标记的大小与缩放级别无关;如果放大,标记大小将保持在10点
  • 数字2相对容易通过使用圆圈而不是标记来修复。(通过使用标记,代码稍微简单一些。)

    然而,另一个问题有点棘手。最可能的情况是,我们希望将圆打包成矩形,使它们的中心尽可能远离彼此。这就是所谓的包装问题,没有简单的解决方案。最佳填料通常相当美观,但即使是很好的近似值,在数学上也相当具有挑战性

    那么,让我们尝试一些更简单的方法。我们将始终在一个有N行的网格中绘制点,每行有N个或N+1个项目

    def find_packing(num_items):
        """Find a reasonable packing of circles into a rectangle
    
        num_items - number of items to pack
    
        Returns: [list of X coordinates], [list of Y coordinates]
                 - lists are normalized so that all coordinates are in the interval [0,1]"""
    
        # get rid of a few special cases:
        if num_items == 0:
            return [],[]
        if num_items == 1:
            return [.5], [.5]
        if num_items == 2:
            return [0., 1.], [.5, .5]
        if num_items == 3:
            return [0., 0., 1.], [0., 1., .5]
    
        # calculate the size of the full square (n >= 2)
        n = int(np.sqrt(num_items))
    
        # determine the required number of rows:
        if num_items > n * (n+1):
            num_rows = n + 1
        else:
            num_rows = n
    
        # determine how may elements we have beyond the full num_rows x n rectangle
        leftovers = num_items - num_rows * n
    
        # let us create a list of row lengths:
        r_lengths = [n] * num_rows
    
        # the leftovers need to be distributed evenly (note the integer division):
        for i in range(leftovers):
            r_lengths[(i * num_rows // leftovers + num_rows / 2) % num_rows] += 1
    
        # let us normalize the coordinates so that the resulting coordinates are always 0,0..1,1
        xcoords = []
        ycoords = []
        row_step = 1. / (num_rows - 1)
        for r in range(num_rows):
            ycoords += [r * row_step] * r_lengths[r]
            xcoords += np.linspace(0, 1, r_lengths[r]).tolist()
    
        return xcoords, ycoords
    
    现在,这很容易集成到绘图代码中:

    # calculate some less random coordinates for zeros and ones
    c_zeros = np.array([.6, .6]) + .8 * np.array(find_packing(n_zeros)).T
    c_ones = np.array([.6, 2.6]) + .8 * np.array(find_packing(n_ones)).T
    
    然后,我们对绘图进行另一个微小的更改,以保持正方形:

        ax.set_aspect('equal')
    
    这应该添加到
    set\u xlim
    命令之前

    通过这些更改,我们的图表如下所示:

    (在这里,我将
    v
    中的项目数更改为200,赔率为0.33/0.67,以使其看起来更有趣。)