Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill between x and baseline x position in Matplotlib

I'm looking for a way to use fill_between in matplotlib to shade between x1 and x2 as opposed to y1 and y2.

I have a series of log plots, with depth on the Y axis, and the measured variable on the x axis and would like to shade to the left or right, as opposed to above or below, of the plotted line.

I'm sure this should be possible with fill_between but cant make it work.

As an example:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec


gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1:, 1])

y=np.random.uniform(0,1,30)
x=np.arange(30)

ax1.set_ylabel('Plot 1')
ax1.plot(x,y)
ax1.fill_between(x,y,0.5,where=y>0.5,interpolate=True)

ax2.set_ylabel('Plot 2')
ax2.plot(y,x)
ax2.set_ylim(30,0)

plt.show()

I have attached an image of what this produces: Essentially i want to plot something like plot 1 in the plot 2 position enter image description here

Thankyou for any suggestions

like image 923
user1665220 Avatar asked Mar 23 '13 17:03

user1665220


People also ask

How do I fill between lines in Matplotlib?

We can fill an area between multiple lines in Matplotlib using the matplotlib. pyplot. fill_between() method. The fill_between() function fills the space between two lines at a time, but we can select one pair of lines to fill the area between multiple lines.

How do I fill two curves in Matplotlib?

fill_between() is used to fill area between two horizontal curves. Two points (x, y1) and (x, y2) define the curves. this creates one or more polygons describing the filled areas.

How do you change X range in Matplotlib?

MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.


1 Answers

you can use fill_betweenx

ax2.fill_betweenx(y,x, x2=0.5, where=x>0.5,interpolate=True)
like image 95
Francesco Montesano Avatar answered Sep 18 '22 07:09

Francesco Montesano