matplotlib: fill between expand shaded area on x-axis

Question:

I want to expand the shaded area from 4.7 to 5 on the x axis (see "example" yellow colored area).
I already figured out how to expand the shaded area on the y axis without an additional point or line.

Example

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

v3_x = np.array([1.7, 1.8, 2.5, 3.4, 4, 4.1, 4.7])
v3_y = np.array([14, 9, 5, 3, 2.5, 1.5, 1]) 

# add reference point 
reference_point_x = 5
reference_point_y = 15

plt.plot(v3_x, v3_y, drawstyle='steps-post', label='steps-post')
plt.plot(v3_x, v3_y, 'o', color='grey', alpha=0.3)
plt.plot(reference_point_x, reference_point_y, 'o--', color='grey', alpha=0.3)

plt.fill_between(v3_x, v3_y, reference_point_y , step="post", alpha=0.4)

plt.show()

I appreciate every hint.

Asked By: Madeira

||

Answers:

You can add a virtual point with numpy.r_:

plt.fill_between(np.r_[v3_x, 5], np.r_[v3_y, 0],
                 reference_point_y , step="post", alpha=0.4)

output:

enter image description here

Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.