-1

I want to shade the area between the two vertical lines over the plot generated by below code

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
data=range(20)
ax.plot(data)
ax.plot(2.5,2.5, marker="|",color='blue',markerfacecolor='none',mew=0.5,ms=10,alpha=0.5)
ax.plot(5.0,5.0, marker="|",color='blue',markerfacecolor='none',mew=0.5,ms=10,alpha=0.5)


plt.show()

I see axvspan but i am unable to solve,hope experts may help me.

1 Answers1

1

I don't know specifically what you want, as your description is a bit vague (it's always helpful to draw what you want in something like Inkscape, powerpoint or even paint when asking questions about graphs), so here's some possible solutions. First, using ax.fill_between.

Here's how to add a bar between your two indicated values:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
data=range(20)
ax.plot(data)
ax.plot(2.5,2.5, marker="|",color='blue',markerfacecolor='none',mew=0.5,ms=10,alpha=0.5)
ax.plot(5.0,5.0, marker="|",color='blue',markerfacecolor='none',mew=0.5,ms=10,alpha=0.5)

ax.fill_between([2.5, 5.0], data[0], data[-1], alpha=0.5)
plt.show()

Results in:

enter image description here

Maybe you want so modify the region a bit, create a smaller square. Modify the fill_between command to this:

ax.fill_between([2.5, 5.0], 2.5, 5.0, alpha=0.5)

enter image description here

Perhaps you want something more like a "confidence interval". Then you'll need to modify your code a bit. Here's an example

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
data=np.arange(0, 20.5, step=0.5)
ax.plot(data, data)
ax.plot(2.5,2.5, marker="|",color='blue',markerfacecolor='none',mew=0.5,ms=10,alpha=0.5)
ax.plot(5.0,5.0, marker="|",color='blue',markerfacecolor='none',mew=0.5,ms=10,alpha=0.5)

ax.fill_between(data, data-1, data+1, alpha=0.5, where=(data >= 2.5) & (data <= 5.0))
plt.show()

enter image description here

EDIT: Now I saw you mentioned axvspan. It's very similar to fill_between. If you replace fill_between in the first example with this:

ax.axvspan(2.5, 5.0, ymin=0, ymax=17, alpha=0.5)

You get this:

enter image description here

K.Cl
  • 1,615
  • 2
  • 9
  • 18
  • if my values are ax.plot(2.5,8.0, marker="|",color='blue',markerfacecolor='none',mew=0.5,ms=10,alpha=0.5) ax.plot(2.5,9.0, marker="|",color='blue',markerfacecolor='none',mew=0.5,ms=10,alpha=0.5) then what to set in fill_between – user19520518 Jul 10 '22 at 19:07
  • The first value is the left edge, second value is right edge. From your snippets, you have the points (2.5, 8.0) and (2.5, 9.0). These have the same x, but different y. This requires `fill_betweenx` or `axhfill`. Use these commands: `ax.fill_betweenx([8, 9], data[0], data[-1], alpha=0.5)` or `ax.axhspan(8, 9, xmin=data[0], xmax=data[-1], alpha=0.5)` – K.Cl Jul 10 '22 at 19:20
  • Thanks @ K.Cl for your answer, if you donot mind can you please have a look into https://stackoverflow.com/questions/72928342/how-to-highlight-the-data-based-on-index-values-in-loop – user19520518 Jul 10 '22 at 19:29
  • if i have many lines and i want to shade with different index values how can it be solved – user19520518 Jul 10 '22 at 19:34
  • Just add more commands to each region you want shaded. Ideally, draw what you want manually and then we can go directly to the point. – K.Cl Jul 10 '22 at 19:36