strange behaviour with fill_between

Dear matplotlibers,

I encountered a bug (?) in fill_between when using logarithmic scales and
the last part of y and yerr arrays as set to zero: a diagonal stripe going from
the rightmost non zero value to the first value is drawn.
It's visible in the right panel of the attached figure, while is not
present if the plot is linear (left panel).
If xaxis is log and yaxis is linear the plot is correctly drawn.

I'm using mpl.__version__ = '1.1.1rc' under Kubuntu 12.04 with Python 2.7.3

The plot has been created with the script below.

Is this a bug or am I missing something?

Cheers
Francesco

##### error_fill_between.py ######
import matplotlib.pyplot as plt
import numpy as np

#values to plot
x = np.linspace( 1, 10, num=100 )
y = np.exp( -x**2 )
y[50:] = 0
yerr = y* np.random.rand(100)

#figure
fig = plt.figure()

ax1 = fig.add_subplot(121) #first axes: linear
ax1.errorbar( x,y,yerr, c='r' )
ax1.fill_between( x,y-yerr,y+yerr, color='b', alpha=0.4 )

ax2 = fig.add_subplot(122) #second axes: logarithmic
ax2.errorbar( x,y,yerr, c='r' )
ax2.fill_between( x,y-yerr,y+yerr, color='b', alpha=0.4 )
ax2.set_xscale( "log" )
ax2.set_yscale( "log" )

plt.show()
###### end script #########

error_fill_between.pdf (15.8 KB)

Dear matplotlibers,

I encountered a bug (?) in fill_between when using logarithmic scales and
the last part of y and yerr arrays as set to zero: a diagonal stripe going from
the rightmost non zero value to the first value is drawn.
It's visible in the right panel of the attached figure, while is not
present if the plot is linear (left panel).
If xaxis is log and yaxis is linear the plot is correctly drawn.

I'm using mpl.__version__ = '1.1.1rc' under Kubuntu 12.04 with Python 2.7.3

The plot has been created with the script below.

Is this a bug or am I missing something?

I don't think it is exactly a bug, but I don't know why the fill region is appearing as it does. The underlying problem is that fill_between is doing what it is told to do without knowing that it is going to be plotted on a log axis.

A good workaround is to change your call to fill_between to look like this:
positive = y - yerr > 0
ax2.fill_between( x,y-yerr,y+yerr, where=positive, color='b', alpha=0.4)

Alternatively, you could use np.clip to put a floor under y - yerr and y + yerr.

Eric

···

On 2012/08/27 5:10 AM, Francesco Montesano wrote:

Cheers
Francesco

##### error_fill_between.py ######
import matplotlib.pyplot as plt
import numpy as np

#values to plot
x = np.linspace( 1, 10, num=100 )
y = np.exp( -x**2 )
y[50:] = 0
yerr = y* np.random.rand(100)

#figure
fig = plt.figure()

ax1 = fig.add_subplot(121) #first axes: linear
ax1.errorbar( x,y,yerr, c='r' )
ax1.fill_between( x,y-yerr,y+yerr, color='b', alpha=0.4 )

ax2 = fig.add_subplot(122) #second axes: logarithmic
ax2.errorbar( x,y,yerr, c='r' )
ax2.fill_between( x,y-yerr,y+yerr, color='b', alpha=0.4 )
ax2.set_xscale( "log" )
ax2.set_yscale( "log" )

plt.show()
###### end script #########

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Dear Eric,

sorry for the delay in replying, and thanking: I forgot the mail after
reading it.

2012/8/30 Eric Firing <efiring@...202...>:

Dear matplotlibers,

I encountered a bug (?) in fill_between when using logarithmic scales and
the last part of y and yerr arrays as set to zero: a diagonal stripe going from
the rightmost non zero value to the first value is drawn.
It's visible in the right panel of the attached figure, while is not
present if the plot is linear (left panel).
If xaxis is log and yaxis is linear the plot is correctly drawn.

I'm using mpl.__version__ = '1.1.1rc' under Kubuntu 12.04 with Python 2.7.3

The plot has been created with the script below.

Is this a bug or am I missing something?

I don't think it is exactly a bug, but I don't know why the fill region
is appearing as it does. The underlying problem is that fill_between is
doing what it is told to do without knowing that it is going to be
plotted on a log axis.

I think that also most of the other plotting functions (e.g. errorbar)
do not know about the scale used on the axis, but they behave
correctly. Am I right?

A good workaround is to change your call to fill_between to look like this:
positive = y - yerr > 0
ax2.fill_between( x,y-yerr,y+yerr, where=positive, color='b', alpha=0.4)

Alternatively, you could use np.clip to put a floor under y - yerr and y
+ yerr.

The workaround works fine, but I dare say that it's not THE solution.
I think that the problem lies in the way PolyCollection is drawn when
y=yerr=0 (and probably also if y+- yerr <=0) if the yaxis is log. I
have no clue where to look in the source to go deeper in the problem.

cheers,
Fra

···

On 2012/08/27 5:10 AM, Francesco Montesano wrote:

Eric

Cheers
Francesco

##### error_fill_between.py ######
import matplotlib.pyplot as plt
import numpy as np

#values to plot
x = np.linspace( 1, 10, num=100 )
y = np.exp( -x**2 )
y[50:] = 0
yerr = y* np.random.rand(100)

#figure
fig = plt.figure()

ax1 = fig.add_subplot(121) #first axes: linear
ax1.errorbar( x,y,yerr, c='r' )
ax1.fill_between( x,y-yerr,y+yerr, color='b', alpha=0.4 )

ax2 = fig.add_subplot(122) #second axes: logarithmic
ax2.errorbar( x,y,yerr, c='r' )
ax2.fill_between( x,y-yerr,y+yerr, color='b', alpha=0.4 )
ax2.set_xscale( "log" )
ax2.set_yscale( "log" )

plt.show()
###### end script #########

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options