使用特定标记进行绘图

发布于 2024-11-17 07:19:02 字数 298 浏览 2 评论 0原文

我想将一个由 0 和 1 组成的矩阵绘制成一个图形,这样对于每个 1,我都有一个形状像垂直条的标记,绘制为“|”。这样,当一系列 1 位于同一 x 轴上时,看起来就像一条长直线。

这个例子说明了我的意图:

给定以下矩阵:

0 0 1 1 0 1 0
0 1 0 1 1 1 0
0 1 0 1 1 1 0
1 0 0 1 1 1 0

我得到:

Bar Figure

I would like to plot a matrix of zeros and ones into a figure such that for every 1 i have a marker shaped like a vertical bar is plotted " | ". Such that when a series of 1s are on the same x axis, the look like a long straight line.

This example illustrates my intentions:

Given the following matrix:

0 0 1 1 0 1 0
0 1 0 1 1 1 0
0 1 0 1 1 1 0
1 0 0 1 1 1 0

I get:

Bar Figure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白况 2024-11-24 07:19:02

编辑:

下面的解决方案虽然比当前接受的解决方案长一点,但其优点是它创建了一个 LINE 对象(如果创建的图形对象较少,UI 性能会更好)。它的工作原理是使用 NaN 来分隔分段:

%#A = [1 1 1 ; 0 0 0 ; 1 1 1];
A = [
    0 0 1 1 0 1 0
    0 1 0 1 1 1 0
    0 1 0 1 1 1 0
    1 0 0 1 1 1 0
];

%# build line x/y points
[m n] = size(A);
[x y] = meshgrid(1:n, 1:m);    %# grid coordinates
x(~A) = NaN;                   %# place NaNs where A is zero
y(~A) = NaN;
x = [x;NaN(1,n)];              %# separate columns by NaNs
y = [y;NaN(1,n)];
x = [x(:) x(:)]';              %'# add endpoints
y = [y(:) y(:)+1]';            %'#
x = x(:);                      %# linearize
y = y(:);

%# plot
line('XData',x, 'YData',y-0.5, 'Color','k', 'LineStyle','-', 'LineWidth',4)
set(gca, 'XGrid','on', 'Box','on', 'FontSize',8, 'LineWidth',2, ...
    'XLim',[0 n]+0.5, 'YLim',[0 m]+0.5, 'XTick',1:n, 'YTick',1:m, ...
    'YDir','reverse')
%#set(gca, 'XTick',[], 'YTick',[])

在此处输入图像描述
在此处输入图像描述

EDIT:

The solution below, although a bit longer than the currently accepted one, has the advantage that it creates a single LINE object (UI performance is better if you create fewer graphics objects). It works by using NaN to separate the segments:

%#A = [1 1 1 ; 0 0 0 ; 1 1 1];
A = [
    0 0 1 1 0 1 0
    0 1 0 1 1 1 0
    0 1 0 1 1 1 0
    1 0 0 1 1 1 0
];

%# build line x/y points
[m n] = size(A);
[x y] = meshgrid(1:n, 1:m);    %# grid coordinates
x(~A) = NaN;                   %# place NaNs where A is zero
y(~A) = NaN;
x = [x;NaN(1,n)];              %# separate columns by NaNs
y = [y;NaN(1,n)];
x = [x(:) x(:)]';              %'# add endpoints
y = [y(:) y(:)+1]';            %'#
x = x(:);                      %# linearize
y = y(:);

%# plot
line('XData',x, 'YData',y-0.5, 'Color','k', 'LineStyle','-', 'LineWidth',4)
set(gca, 'XGrid','on', 'Box','on', 'FontSize',8, 'LineWidth',2, ...
    'XLim',[0 n]+0.5, 'YLim',[0 m]+0.5, 'XTick',1:n, 'YTick',1:m, ...
    'YDir','reverse')
%#set(gca, 'XTick',[], 'YTick',[])

enter image description here
enter image description here

無心 2024-11-24 07:19:02

解决方案2

这是另一个解决方案,看起来很简单。每个数字由一条垂直线表示。所有内容都集中在一个情节陈述中。

%# create the matrix and get coordinates of 1s.
a = logical([
0 0 1 1 0 1 0
0 1 0 1 1 1 0
0 1 0 1 1 1 0
1 0 0 1 1 1 0]);
[r c] = find(flipud(a));
plot([c c]',[r-0.5 r+0.5]','k-')
xlim([min(c)-0.5 max(c)+0.5])
set(gca,'xtick',[],'ytick',[])
box on

在此处输入图像描述


解决方案 1

作为替代方案,您可以使用 TEXT 函数来放置“|”特定坐标处的符号。

[r c] = find(flipud(a));
clf
text(c,r,repmat('|',numel(r),1),'FontSize',70,'hor','center','vert','middle')
xlim([min(c)-0.5 max(c)+0.5])
ylim([min(r)-0.6 max(r)+0.4])
set(gca,'xtick',[],'ytick',[])
box on

缺点是您必须调整字体大小和 y 轴限制才能关闭线条。

在此处输入图像描述

旁注:很奇怪,我不能只使用 '| '无需重新格式化。因为这个字符实际上可以分隔不同的字符串。使用 char(124) 具有相同的效果。我想知道是否还有其他解决方法。

SOLUTION 2

Here is another solution, which looks quite simple. Each number represented by a single vertical line. All in one plot statement.

%# create the matrix and get coordinates of 1s.
a = logical([
0 0 1 1 0 1 0
0 1 0 1 1 1 0
0 1 0 1 1 1 0
1 0 0 1 1 1 0]);
[r c] = find(flipud(a));
plot([c c]',[r-0.5 r+0.5]','k-')
xlim([min(c)-0.5 max(c)+0.5])
set(gca,'xtick',[],'ytick',[])
box on

enter image description here


SOLUTION 1

As an alternative you can use TEXT function to place '|' symbol at certain coordinates.

[r c] = find(flipud(a));
clf
text(c,r,repmat('|',numel(r),1),'FontSize',70,'hor','center','vert','middle')
xlim([min(c)-0.5 max(c)+0.5])
ylim([min(r)-0.6 max(r)+0.4])
set(gca,'xtick',[],'ytick',[])
box on

The drawback is that you have to play with the font size and y axis limits to close the lines.

enter image description here

Side note: It's weird, that I couldn't use just '|' without repmat. Because this character can actually separate different strings. Using char(124) has the same effect. I wonder if there is any other workaround.

始终不够爱げ你 2024-11-24 07:19:02

这是一种方法,将 1 转换为显式点并通过它们画一条线:

B=logical([A(1,:);A;A(end,:)]);    %# A is your matrix of 1's and 0's

%# create a mesh of indices
x=1:size(B,2);
y=0:size(A,1)+1;
[X,Y]=meshgrid(x,y);

%# plot the lines
figure(1);clf;hold on
arrayfun(@(i)plot(X(B(:,i),i)',Y(B(:,i),i)','color','k','linewidth',1.25),x)
hold off 
set(gca,'box','on','xlim',[min(x),max(x)]+[-1/2 1/2],...
'ydir','r','ytick',[])

这是您应该得到的:

在此处输入图像描述

您可能可以取消 arrayfun,但如果您愿意,我会将其留给您。

Here is one way of doing it by converting the 1's to explicit points and drawing a line through them:

B=logical([A(1,:);A;A(end,:)]);    %# A is your matrix of 1's and 0's

%# create a mesh of indices
x=1:size(B,2);
y=0:size(A,1)+1;
[X,Y]=meshgrid(x,y);

%# plot the lines
figure(1);clf;hold on
arrayfun(@(i)plot(X(B(:,i),i)',Y(B(:,i),i)','color','k','linewidth',1.25),x)
hold off 
set(gca,'box','on','xlim',[min(x),max(x)]+[-1/2 1/2],...
'ydir','r','ytick',[])

Here is what you should get:

enter image description here

You can probably do away with the arrayfun, but I'll leave that to you if you so wish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文