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 |
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 |
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.On 24 November 2017 at 02:42, dimitrif <[hidden email]> wrote: Hi to all, _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users |
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 |
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 |
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.1) Exclude the x=0 values from the plot, i.e. use cf1 = plt.contourf(np.log10(f_psd_ cf1 = plt.contourf(np.log10(f_psd_ 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. On 24 November 2017 at 12:53, dimitrif <[hidden email]> wrote: Hi Ian, _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users |
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 |
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)) Ianplt.gca().set_xscale('log') plt.xlim([np.min(f_psd_welch[1:]), np.max(f_psd_welch[1:])]) On 24 November 2017 at 20:03, dimitrif <[hidden email]> wrote: Hi Ian, _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users |
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 |
Free forum by Nabble | Edit this page |