Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Numpy Beginner's Guide (Update)
  • Toc
  • feedback
Numpy Beginner's Guide (Update)

Numpy Beginner's Guide (Update)

By : Ivan Idris
2 (1)
close
Numpy Beginner's Guide (Update)

Numpy Beginner's Guide (Update)

2 (1)
By: Ivan Idris

Overview of this book

This book is for the scientists, engineers, programmers, or analysts looking for a high-quality, open source mathematical library. Knowledge of Python is assumed. Also, some affinity, or at least interest, in mathematics and statistics is required. However, I have provided brief explanations and pointers to learning resources.
Table of Contents (16 chapters)
close
14
C. NumPy Functions' References
15
Index

Time for action – shading plot regions based on a condition

Imagine that you want to shade a region of a stock chart, where the closing price is below average, with a different color than when it is above the mean. The fill_between() function is the best choice for the job. We will, again, omit the steps of downloading historical data going back one year, extracting dates and close prices, and creating locators and date formatter.

  1. Create a matplotlib Figure object:
    fig = plt.figure()
  2. Add a subplot to the figure:
    ax = fig.add_subplot(111)
  3. Plot the closing price:
    ax.plot(dates, close)
  4. Shade the regions of the plot below the closing price using different colors depending on whether the values are below or above the average price:
    plt.fill_between(dates, close.min(), close, where=close>close.mean(), facecolor="green", alpha=0.4)
    plt.fill_between(dates, close.min(), close, where=close<close.mean(), facecolor="red", alpha=0.4)

    Now we can finish the plot as shown by setting...

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete