 # Plotting 2d fourier amplitudes

Hello,

I have 2d array with fourier amplitudes that I would like to plot. I
found two options: contourf and imshow. This is my code:

omega = np.fft.rfftn(b_field, axes=(1, 0))
omega = np.abs(np.fft.fftshift(omega, axes=(1,)))
fig = plt.figure()
M = omega.shape
N = omega.shape
ax.set_title('Spectrum')
ax.set_ylabel(r'Poloidal Mode Number m')
ax.set_xlabel(r'Toroidal Mode Number n')
ax.grid(True)

# Get rid of normalization
omega /= np.prod(omega.shape)

The problem with contourf is that I can't seem to stop it from
strongly interpolating the data, which obscures the discrete nature:
(see www.rath.org/contourf.png)

ctr = ax.contourf(np.arange(-N / 2, N / 2),
np.arange(0, M),
omega * 10000, 100, cmap=cm.YlOrRd, interpolation='nearest')
fig.colorbar(ctr)
ax.set_xlim(xmin= -(N - 1) / 2, xmax=(N - 1) / 2)
ax.set_ylim(ymin=0, ymax=M - 1)
fig.show()

Apparently contourf does not accept the interpolation='nearest' option.
Is there a way to make it stop interpolating?

The problem with imshow is, that it rescales the data so the
colorbar does not show the correct amplitudes (see
www.rath.org/imshow.png):

ctr = ax.imshow(omega, cmap=cm.YlOrRd, aspect='equal', interpolation='nearest',
origin='lower', extent=(-(N-1)/2, (N-1)/2, 0, M-1))
fig.colorbar(ctr)
ax.set_xlim(xmin= -(N - 1) / 2, xmax=(N - 1) / 2)
ax.set_ylim(ymin=0, ymax=M - 1)
fig.show()

Is there a way to get the proper amplitudes into the colorbar?

Thanks!

-Nikolaus

···

--
»Time flies like an arrow, fruit flies like a Banana.«

PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C

Did you try to change vmin, vmax?

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshow

If this is not what you want, please describe more explicitly why the
colorbar is wrong.

Regards,

-JJ

···

On Mon, Apr 26, 2010 at 11:37 AM, Nikolaus Rath <Nikolaus@...3072...> wrote:

The problem with imshow is, that it rescales the data so the
colorbar does not show the correct amplitudes (see
www.rath.org/imshow.png):

ctr = ax.imshow(omega, cmap=cm.YlOrRd, aspect='equal', interpolation='nearest',
origin='lower', extent=(-(N-1)/2, (N-1)/2, 0, M-1))
fig.colorbar(ctr)
ax.set_xlim(xmin= -(N - 1) / 2, xmax=(N - 1) / 2)
ax.set_ylim(ymin=0, ymax=M - 1)
fig.show()

Is there a way to get the proper amplitudes into the colorbar?

Hi,

I want the colorbar to show the real fourier amplitudes, not the
amplitudes after rescaling to 0..1. In other words: the colorbar created
by imshow should have the same values as the one created by contourf.

I am not sure how I can use vmin and vmax to achieve that effect. The
scaling is done correctly, it's just that the colorbar should show the
original, and not the rescaled values.

Thanks,
-Niko

I did not try vmin and vmax, because

···

On 04/26/2010 02:31 PM, Jae-Joon Lee wrote:

Did you try to change vmin, vmax?

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshow

If this is not what you want, please describe more explicitly why the
colorbar is wrong.

Regards,

-JJ

On Mon, Apr 26, 2010 at 11:37 AM, Nikolaus Rath <Nikolaus@...3072...> wrote:

The problem with imshow is, that it rescales the data so the
colorbar does not show the correct amplitudes (see
www.rath.org/imshow.png):

ctr = ax.imshow(omega, cmap=cm.YlOrRd, aspect='equal', interpolation='nearest',
origin='lower', extent=(-(N-1)/2, (N-1)/2, 0, M-1))
fig.colorbar(ctr)
ax.set_xlim(xmin= -(N - 1) / 2, xmax=(N - 1) / 2)
ax.set_ylim(ymin=0, ymax=M - 1)
fig.show()

Is there a way to get the proper amplitudes into the colorbar?

-Nikolaus

--
»Time flies like an arrow, fruit flies like a Banana.«

PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C

I think I'm lost.

In other words: the colorbar created
by imshow should have the same values as the one created by contourf.

with contourf, you're drawing "omega*10000", but with imshow, you're
drawing "omega".
Are you saying that these two should have same colorbar, although
there is a factor 10000 difference?

And, as far as I know, colorbar shows values of the original input.

What is the min and max of omega, and what do you expect your colorbar show?
If possible, please post a complete example so that others can test.

Regards,

-JJ

···

I am not sure how I can use vmin and vmax to achieve that effect. The
scaling is done correctly, it's just that the colorbar should show the
original, and not the rescaled values.

Thanks,
-Niko

I did not try vmin and vmax, because
On 04/26/2010 02:31 PM, Jae-Joon Lee wrote:

Did you try to change vmin, vmax?

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshow

If this is not what you want, please describe more explicitly why the
colorbar is wrong.

Regards,

-JJ

On Mon, Apr 26, 2010 at 11:37 AM, Nikolaus Rath <Nikolaus@...3072...> wrote:

The problem with imshow is, that it rescales the data so the
colorbar does not show the correct amplitudes (see
www.rath.org/imshow.png):

ctr = ax.imshow(omega, cmap=cm.YlOrRd, aspect='equal', interpolation='nearest',
origin='lower', extent=(-(N-1)/2, (N-1)/2, 0, M-1))
fig.colorbar(ctr)
ax.set_xlim(xmin= -(N - 1) / 2, xmax=(N - 1) / 2)
ax.set_ylim(ymin=0, ymax=M - 1)
fig.show()

Is there a way to get the proper amplitudes into the colorbar?

-Nikolaus

--
»Time flies like an arrow, fruit flies like a Banana.«

PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C

Hi,

Believe it or not, the factor of 10000 was my only problem all the time.
Until you pointed it out I never noticed that it was missing in one of
the calls.

Thanks!
Niko

···

On 04/26/2010 03:26 PM, Jae-Joon Lee wrote:

I think I'm lost.

In other words: the colorbar created
by imshow should have the same values as the one created by contourf.

with contourf, you're drawing "omega*10000", but with imshow, you're
drawing "omega".
Are you saying that these two should have same colorbar, although
there is a factor 10000 difference?

And, as far as I know, colorbar shows values of the original input.

What is the min and max of omega, and what do you expect your colorbar show?
If possible, please post a complete example so that others can test.

Regards,

-JJ

I am not sure how I can use vmin and vmax to achieve that effect. The
scaling is done correctly, it's just that the colorbar should show the
original, and not the rescaled values.

Thanks,
-Niko

I did not try vmin and vmax, because
On 04/26/2010 02:31 PM, Jae-Joon Lee wrote:

Did you try to change vmin, vmax?

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshow

If this is not what you want, please describe more explicitly why the
colorbar is wrong.

Regards,

-JJ

On Mon, Apr 26, 2010 at 11:37 AM, Nikolaus Rath <Nikolaus@...3072...> wrote:

The problem with imshow is, that it rescales the data so the
colorbar does not show the correct amplitudes (see
www.rath.org/imshow.png):

ctr = ax.imshow(omega, cmap=cm.YlOrRd, aspect='equal', interpolation='nearest',
origin='lower', extent=(-(N-1)/2, (N-1)/2, 0, M-1))
fig.colorbar(ctr)
ax.set_xlim(xmin= -(N - 1) / 2, xmax=(N - 1) / 2)
ax.set_ylim(ymin=0, ymax=M - 1)
fig.show()

Is there a way to get the proper amplitudes into the colorbar?

-Nikolaus

--
»Time flies like an arrow, fruit flies like a Banana.«

PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C

-Nikolaus

--
»Time flies like an arrow, fruit flies like a Banana.«

PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C