log xaxis for contourf

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

log xaxis for contourf

dimitrif
Hi to all,

I spent about 3 hours trying to fix a plotting problem I have with
matplotlib.pyplot...unfortunately without any sucess.

I want to have a logarithmic xaxis in my contourf plot. Since this is not
directly possible, I took the log10 of the axis and simply used them. Doing
so, I get some white space inside the plot for some reason. Can someone give
me a hint why this is happening? I use the following fraction of code:

#without log scale
plt.figure(11), plt.cla()
cf1 = plt.contourf(f_psd_welch, traverseCoord,
10*np.log10(psd_hotwire_FS_welch),cmap='viridis')
cbar1 = plt.colorbar(cf1)
plt.xlim([min(f_psd_welch), max(f_psd_welch)]), plt.ylim([-50, 50])

<http://matplotlib.1069221.n5.nabble.com/file/t5112/normalX.png>

#with logscale
plt.figure(11), plt.cla()
cf1 = plt.contourf(np.log10(f_psd_welch), traverseCoord,
10*np.log10(psd_hotwire_FS_welch),cmap='viridis')
cbar1 = plt.colorbar(cf1)
plt.xlim([min(np.log10(f_psd_welch[1:])), max(np.log10(f_psd_welch[1:]))]),
plt.ylim([-50, 50])

<http://matplotlib.1069221.n5.nabble.com/file/t5112/logX.png>

The output is attached.
I would be happy about any help!

Thanks,
Dimitri



--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: log xaxis for contourf

Jody Klymak

You are giving contour very irregularly spaced data in x.  Its trying to come up w/ those last few contours with very little data and I guess its misbehaving.

If it were me, I’d interpolate onto a regular log10 grid first and then send to contour.  Then you have control over how the interpolation takes place...

You may also want to consider just using pcolormesh.  

Cheers,  Jody

> On Nov 23, 2017, at  18:42 PM, dimitrif <[hidden email]> wrote:
>
> Hi to all,
>
> I spent about 3 hours trying to fix a plotting problem I have with
> matplotlib.pyplot...unfortunately without any sucess.
>
> I want to have a logarithmic xaxis in my contourf plot. Since this is not
> directly possible, I took the log10 of the axis and simply used them. Doing
> so, I get some white space inside the plot for some reason. Can someone give
> me a hint why this is happening? I use the following fraction of code:
>
> #without log scale
> plt.figure(11), plt.cla()
> cf1 = plt.contourf(f_psd_welch, traverseCoord,
> 10*np.log10(psd_hotwire_FS_welch),cmap='viridis')
> cbar1 = plt.colorbar(cf1)
> plt.xlim([min(f_psd_welch), max(f_psd_welch)]), plt.ylim([-50, 50])
>
> <http://matplotlib.1069221.n5.nabble.com/file/t5112/normalX.png>
>
> #with logscale
> plt.figure(11), plt.cla()
> cf1 = plt.contourf(np.log10(f_psd_welch), traverseCoord,
> 10*np.log10(psd_hotwire_FS_welch),cmap='viridis')
> cbar1 = plt.colorbar(cf1)
> plt.xlim([min(np.log10(f_psd_welch[1:])), max(np.log10(f_psd_welch[1:]))]),
> plt.ylim([-50, 50])
>
> <http://matplotlib.1069221.n5.nabble.com/file/t5112/logX.png>
>
> The output is attached.
> I would be happy about any help!
>
> Thanks,
> Dimitri
>
>
>
> --
> Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
> _______________________________________________
> Matplotlib-users mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: log xaxis for contourf

Ian Thomas-8
In reply to this post by dimitrif
Dimitri,

If you send me your data (f_psd_welch, traverseCoord and psd_hotwire_FS_welch) I will take a look at the problem.

Ian

On 24 November 2017 at 02:42, dimitrif <[hidden email]> wrote:
Hi to all,

I spent about 3 hours trying to fix a plotting problem I have with
matplotlib.pyplot...unfortunately without any sucess.

I want to have a logarithmic xaxis in my contourf plot. Since this is not
directly possible, I took the log10 of the axis and simply used them. Doing
so, I get some white space inside the plot for some reason. Can someone give
me a hint why this is happening? I use the following fraction of code:

#without log scale
plt.figure(11), plt.cla()
cf1 = plt.contourf(f_psd_welch, traverseCoord,
10*np.log10(psd_hotwire_FS_welch),cmap='viridis')
cbar1 = plt.colorbar(cf1)
plt.xlim([min(f_psd_welch), max(f_psd_welch)]), plt.ylim([-50, 50])

<http://matplotlib.1069221.n5.nabble.com/file/t5112/normalX.png>

#with logscale
plt.figure(11), plt.cla()
cf1 = plt.contourf(np.log10(f_psd_welch), traverseCoord,
10*np.log10(psd_hotwire_FS_welch),cmap='viridis')
cbar1 = plt.colorbar(cf1)
plt.xlim([min(np.log10(f_psd_welch[1:])), max(np.log10(f_psd_welch[1:]))]),
plt.ylim([-50, 50])

<http://matplotlib.1069221.n5.nabble.com/file/t5112/logX.png>

The output is attached.
I would be happy about any help!

Thanks,
Dimitri



--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: log xaxis for contourf

dimitrif
In reply to this post by Jody Klymak
Hi Jody,

I have tried pcolormesh but it 'smears' the data too much due to
interpolation.

Maybe there is no other way than interpolating manually to have full
control. I just wonder, it seems like it works in Matlab without any issues
but matpotlib struggles with log axis for contourf.

Dimitri



--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: log xaxis for contourf

dimitrif
In reply to this post by Ian Thomas-8
Hi Ian,

I sent you an email.

Dimitir



--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: log xaxis for contourf

Ian Thomas-8
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 <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: log xaxis for contourf

dimitrif
Hi Ian,

Amazing, thank you very much for your help! Now, it works as expected.

I actually got the following warning 'RuntimeWarning: divide by zero
encountered in log10' when running the script but I couldn't connect it to
the outcome. Sometimes a new pair of eyes and a fresh mind helps to solve
the issue in a few seconds.

Sounds logically for me that changing the library would not be necessary. I
wonder why contourf does not have the option to switch the axes to log
similar to loglog or semilogx? That would look much nicer and save a lot of
time adjusting the axes of contourf.

Dimitri



--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: log xaxis for contourf

Ian Thomas-8
Dimitri,

Although there is no explicit 'log axis' argument that contour accepts, you can use this:

plt.contourf(f_psd_welch, traverseCoord, 10*np.log10(psd_hotwire_FS_welch))
plt.gca().set_xscale('log')
plt.xlim([np.min(f_psd_welch[1:]), np.max(f_psd_welch[1:])])

Ian

On 24 November 2017 at 20:03, dimitrif <[hidden email]> wrote:
Hi Ian,

Amazing, thank you very much for your help! Now, it works as expected.

I actually got the following warning 'RuntimeWarning: divide by zero
encountered in log10' when running the script but I couldn't connect it to
the outcome. Sometimes a new pair of eyes and a fresh mind helps to solve
the issue in a few seconds.

Sounds logically for me that changing the library would not be necessary. I
wonder why contourf does not have the option to switch the axes to log
similar to loglog or semilogx? That would look much nicer and save a lot of
time adjusting the axes of contourf.

Dimitri



--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: log xaxis for contourf

dimitrif
Hi Ian,

I didn't know how to do that. That is actuaclly exactly what I want. Thank
you very much once again.

Dimitri



--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users