开发者

Adding arrows to rising edges of pulse plots in matplotlib

开发者 https://www.devze.com 2023-03-29 09:49 出处:网络
Need help in matplotlib to figure out the answers for, How do you make an arrow pointing up and an arrow pointing down along a curve/line on the plot?

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 highe开发者_运维百科st 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:

Adding arrows to rising edges of pulse plots in matplotlib

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

Adding arrows to rising edges of pulse plots in matplotlib

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:

Adding arrows to rising edges of pulse plots in matplotlib

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号