在 matplotlib 中向脉冲图的上升沿添加箭头

发布于 2024-11-30 13:27:08 字数 1904 浏览 3 评论 0原文

需要 matplotlib 中的帮助来找出以下问题的答案:

  1. 如何沿着绘图上的曲线/直线制作一个向上的箭头和一个向下的箭头?

  2. 如何调整图的范围/限制,使其边缘在图表上清晰可见?

我想在脉冲波的上升沿上添加箭头(脉冲下降沿上的向下箭头将是一个额外的好处),但无法找到一种简单的方法来做到这一点(注释页面上的示例提供了完全不同的目的)。

由于图片使事情变得更清晰,我的代码是:

from pylab import *

# How do I have arrows pointing up for every even value and pointing down for every odd value along Y axis for the highest freq.

baseFreqTimePeriod = 100
samplingFreqTimePeriod = 0.001

numberOfPulses = 10

def baseFreq(inputTime, timePeriod):

    sampleWindow = timePeriod

    return [ (1.0/(timePeriod/baseFreqTimePeriod)) if((i % sampleWindow) <= (sampleWindow / 2)) else 0.0 for i in inputTime ]

    '''
    output = []

    for i in inputTime:

        sampleIsAtPosition = i % sampleWindow

        if(sampleIsAtPosition <= (sampleWindow / 2)):

            output.append(1.0/(timePeriod/baseFreqTimePeriod))
        else:
            output.append(0.0)

    return output
    '''

t = arange(0.0, numberOfPulses * baseFreqTimePeriod, samplingFreqTimePeriod)

baseFreqFn = baseFreq(t, baseFreqTimePeriod)
plot(t, baseFreqFn, linewidth=1.0)

baseFreqFn = baseFreq(t, 2 * baseFreqTimePeriod)
plot(t, baseFreqFn, linewidth=2.0)

baseFreqFn = baseFreq(t, 3 * baseFreqTimePeriod)
plot(t, baseFreqFn, linewidth=2.0)

xlabel('time (uS)')
ylabel('Amplitude')
title('Test')
grid(True)
show()

这会生成输出:

在此处输入图像描述

相反,这会让我happy (只绘制了前几个箭头)

在此处输入图像描述

另外,请注意图形的显示窗口如何使顶部全振幅波剪辑? (底部位也被遮挡)

如果我能看到峰峰值(轴超出数据范围),那就太好了,如下所示:

在此处输入图像描述 (http://web.mit.edu/6.02/www/f2009/handouts/labs/lab2_7_1.png

Need help in matplotlib to figure out the answers for,

  1. How do you make an arrow pointing up and an arrow pointing down along a curve/line on the plot?

  2. How do you adjust the range/limits of the plot so that the edges of it are visible clearly on the graph?

I wanted to add arrows on the rising edge of a pulsing wave, (a down arrow on the falling edges of the pulse would be a bonus) and could not figure out an easy way to do it (the examples on the annotation page serve a different purpose altogether).

Since a picture makes things clearer, my code is:

from pylab import *

# How do I have arrows pointing up for every even value and pointing down for every odd value along Y axis for the highest freq.

baseFreqTimePeriod = 100
samplingFreqTimePeriod = 0.001

numberOfPulses = 10

def baseFreq(inputTime, timePeriod):

    sampleWindow = timePeriod

    return [ (1.0/(timePeriod/baseFreqTimePeriod)) if((i % sampleWindow) <= (sampleWindow / 2)) else 0.0 for i in inputTime ]

    '''
    output = []

    for i in inputTime:

        sampleIsAtPosition = i % sampleWindow

        if(sampleIsAtPosition <= (sampleWindow / 2)):

            output.append(1.0/(timePeriod/baseFreqTimePeriod))
        else:
            output.append(0.0)

    return output
    '''

t = arange(0.0, numberOfPulses * baseFreqTimePeriod, samplingFreqTimePeriod)

baseFreqFn = baseFreq(t, baseFreqTimePeriod)
plot(t, baseFreqFn, linewidth=1.0)

baseFreqFn = baseFreq(t, 2 * baseFreqTimePeriod)
plot(t, baseFreqFn, linewidth=2.0)

baseFreqFn = baseFreq(t, 3 * baseFreqTimePeriod)
plot(t, baseFreqFn, linewidth=2.0)

xlabel('time (uS)')
ylabel('Amplitude')
title('Test')
grid(True)
show()

This generates the output:

enter image description here

Instead, this would make me happy (only first few arrows drawn)

enter image description here

Also, notice how the display window of the graph makes the top of the full amplitude wave clip? (bottom bits are obscured as well)

It would be great if I could see the peak-to-peak values (the axis extend beyond the range of the data), like in here:

enter image description here (http://web.mit.edu/6.02/www/f2009/handouts/labs/lab2_7_1.png)

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

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

发布评论

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

评论(1

友欢 2024-12-07 13:27:08

要制作箭头,您可以使用 arrow 例程:

     #x,y, dx, dy,
arrow(0,0,  0,1.0,
    'lw':2,
    'color':'black',
    'head_width':baseFreqTimePeriod/4,
    'head_length':0.05

要制作向下的箭头,请设置负 dy。:

arrow(baseFreqTimePeriod,0,  0,-1.0,
    'lw':2,
    'color':'black',
    'head_width':baseFreqTimePeriod/4,
    'head_length':0.05

要更改绘图的限制,请使用 xlim 和`ylim':

ylim([-.2,1.2])
xlim([-.1*t[-1],t[-1]*1.1])

有关于 arrow 的文档,xlimylim。还可以使用 注释 方法。

To make an arrow, you can use the arrow routine:

     #x,y, dx, dy,
arrow(0,0,  0,1.0,
    'lw':2,
    'color':'black',
    'head_width':baseFreqTimePeriod/4,
    'head_length':0.05

to make a downward pointing arrow, set a negative dy.:

arrow(baseFreqTimePeriod,0,  0,-1.0,
    'lw':2,
    'color':'black',
    'head_width':baseFreqTimePeriod/4,
    'head_length':0.05

To change the limits of the plot, use xlim and `ylim':

ylim([-.2,1.2])
xlim([-.1*t[-1],t[-1]*1.1])

There is documentation on arrow, xlim and ylim. It is also possible to make an arrow with text, using the annotate method.

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