使用特定标记进行绘图
我想将一个由 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
我得到:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:
下面的解决方案虽然比当前接受的解决方案长一点,但其优点是它创建了一个 LINE 对象(如果创建的图形对象较少,UI 性能会更好)。它的工作原理是使用
NaN
来分隔分段: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:解决方案2
这是另一个解决方案,看起来很简单。每个数字由一条垂直线表示。所有内容都集中在一个情节陈述中。
解决方案 1
作为替代方案,您可以使用 TEXT 函数来放置“|”特定坐标处的符号。
缺点是您必须调整字体大小和 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.
SOLUTION 1
As an alternative you can use TEXT function to place '|' symbol at certain coordinates.
The drawback is that you have to play with the font size and y axis limits to close the lines.
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.这是一种方法,将 1 转换为显式点并通过它们画一条线:
这是您应该得到的:
您可能可以取消
arrayfun
,但如果您愿意,我会将其留给您。Here is one way of doing it by converting the 1's to explicit points and drawing a line through them:
Here is what you should get:
You can probably do away with the
arrayfun
, but I'll leave that to you if you so wish.