[Matplotlib-users] log xaxis for contourf

Ian Thomas ianthomas23 at gmail.com
Fri Nov 24 14:46:59 EST 2017


Hi Dimitri,

The problem is that your x data (f_psd_welch) begins at zero. Log10 of zero
is minus infinity. Some of the contour x-values will be exactly zero and
hence log10 of them is -inf; most will be larger than zero and hence log10
of them will be finite. Drawing polygons between finite and infinite values
is bound to look messy.

There is usually a numpy warning when you try np.log10(0.0), but it depends
on some python settings so you may not have seen it.

You had nearly solved the problem yourself as when you are calculating the
minimum x-value to limit the contour plot axis you use
np.min(np.log10(f_psd_welch[1:])) which excludes the first value which is
log10(0.0) = -inf.

There are 2 possible solutions:

1) Exclude the x=0 values from the plot, i.e. use
cf1 = plt.contourf(np.log10(f_psd_welch[1:]), traverseCoord,
10*np.log10(psd_hotwire_FS_welch[:,1:]),cmap='viridis')

2) Add a very small amount to all x-values, i.e.
cf1 = plt.contourf(np.log10(f_psd_welch + 1e-10), traverseCoord,
10*np.log10(psd_hotwire_FS_welch),cmap='viridis')

We could add an explicit check to ensure that all arrays that are passed to
contour contain only finite values, but then we would have to have a policy
of doing that across all matplotlib functions which would be a major impact
to the library and (arguably) unnecessarily affect the performance of 99.9%
of mpl calls that don't need the check. The existing numpy log10 warning
should suffice.

Ian

On 24 November 2017 at 12:53, dimitrif <df388 at cam.ac.uk> wrote:

> Hi Ian,
>
> I sent you an email.
>
> Dimitir
>
>
>
> --
> Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-
> f3.html
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20171124/ffb0347e/attachment.html>


More information about the Matplotlib-users mailing list