Stem plot in matplotlib?

前端 未结 2 788
迷失自我
迷失自我 2020-12-31 05:05

I want to plot(x, sin(x)) but instead of a line from (xi,yi) to (x_i+1,y_i+1) I want a vertical line to each point from (xi,0)

相关标签:
2条回答
  • 2020-12-31 05:27

    I think you want to use linestyle='steps--'

    plot(x, sin(x), linestyle='steps--')
    
    0 讨论(0)
  • 2020-12-31 05:39

    There is a stem plot (a.k.a. lollipop plot) in the matplotlib as well.


    Below you can find an example from the docs.

    import matplotlib.pyplot as plt
    import numpy as np
    
    # returns 10 evenly spaced samples from 0.1 to 2*PI
    x = np.linspace(0.1, 2 * np.pi, 10)
    
    markerline, stemlines, baseline = plt.stem(x, np.cos(x), '-.')
    
    # setting property of baseline with color red and linewidth 2
    plt.setp(baseline, color='r', linewidth=2)
    
    plt.show()
    

    stem plot


    If you want to adjust the y-position of the baseline, you can use the bottom parameter.

    (adapted) Example:

    import matplotlib.pyplot as plt
    import numpy as np
    
    # returns 10 evenly spaced samples from 0.1 to 2*PI
    x = np.linspace(0.1, 2 * np.pi, 10)
    
    plt.stem(x, np.cos(x), '-.', bottom=-2)
    
    plt.show()
    

    stem plot 2

    0 讨论(0)
提交回复
热议问题