Slartibartfast
Slartibartfast

Reputation: 1200

matplotlib - plotting a straight line

Given the following section of code:

    fig, (ax1, ax2) = plt.subplots(2, 1)
    num = sto_list.gt(70).sum(1)
    plt.yticks(fontsize = 25)
    df2 = web.DataReader('fb', 'yahoo', start, end)
    ax = num.plot(figsize=(45,25), ax=ax2, color = 'Red')
    df2.plot(y = 'Close', figsize=(45,25), ax=ax1, color = 'Green')
    ax.grid()
    ax1.xaxis.label.set_visible(False)
    ax.xaxis.label.set_visible(False)

This produces a chart which looks like this: enter image description here

The subplot at the bottom is plotted from num:

num
Out[70]: 
Date
2015-07-06    33
2015-07-07    20
2015-07-08     4
2015-07-09     8
2015-07-10     8
              ..
2020-06-29    14
2020-06-30    13
2020-07-01    18
2020-07-02    20
2020-07-03    28
Length: 1228, dtype: int64

What i want to do is plot a straight line wherever it is less than 10 with this:

plt.axvline(x=num.lt(10), ax = ax2)

I am not able to plot the line though. What would be the best way in doing so?

Upvotes: 0

Views: 283

Answers (1)

cfort
cfort

Reputation: 2776

The problem is that num.lt returns a series and axvline wants a scalar.

Try looping through and drawing a line for each index value:

dates = num[num.lt(10)].index
for d in dates:
    ax2.axvline(d)       

Upvotes: 1

Related Questions