Reputation: 499
I've looked at a few posts on this but none have worked so far. I have a pandas dataframe, wp_df (excuse the bad column formatting, from copy/paste, but you get the idea):
wp game_seconds_remaining game_half
18906 0.901082 0.0 Overtime
18905 0.932920 22.0 Overtime
18904 0.688796 28.0 Overtime
18903 0.735589 32.0 Overtime
18902 0.730993 72.0 Overtime
... ... ... ...
18749 0.471634 3664.0 Half1
18748 0.513039 3702.0 Half1
18747 0.541705 3743.0 Half1
18746 0.541372 3776.0 Half1
18745 0.564195 3813.0 Half1
I have graphed a scatter plot line from wp_df['wp'] using plt.plot(), resulting in the following image:
I added some horizontal and vertical lines for visual ease. I would like to shade in regions between the red scatterplot line and the blue horizontal line at y = 0.50. --> Note that I have changed the x and y ticks. X ranges from the max values of wp_df['game_seconds_remaining] (in this case 3813.0) to 0.0 (the x axis is flipped), and y ranges from 0.0 to 1.0.
Anyhow, I want to shade the region between the red scatter line and the horizontal line at 0.50 on both sides of the horizontal line, with a different color on each. I tried going for plt.fill_between(wp_df['wp'], 0.50, color='blue')
for one side of the shading, but this did not work out.
Any suggestions for how I could make this work?
Upvotes: 0
Views: 261
Reputation: 8800
Just filling in a little extra based on your comment with JohanC. You can use the where
condition of fill_between
to specify the specific blocks to fill. There's a nice example here, but here's an example more similar to your data:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = range(200)
y = np.concatenate((np.random.randint(0,50,50), np.random.randint(50,100,50),
np.random.randint(0,50,50), np.random.randint(50,100,50)))
fig, ax = plt.subplots(figsize=(7,3.5))
ax.plot(x, y, color='red')
ax.axhline(50,0,200,color='blue')
ax.fill_between(x,y,50,where=(y<50), alpha=.3, color='orange')
ax.fill_between(x,y,50,where=(y>=50), alpha=.3, color='blue')
So if you have one condition with two options (i.e. above or below 50), you can use two calls to fill_between
. y<50
and y>=50
are boolean arrays denoting the points where y
is above and below 50. So note that the two calls fill 4 different areas of the plot here.
The code from @JohanC in the comment should be simply modified to replicate the above!
Upvotes: 1