Victor Kärcher
Victor Kärcher

Reputation: 19

How to change the axis dimension from pixel to length in matplotlib? is there any code in general?

Since the complete simulation is to big to post it right here only the code to plot the spectrum is given (I think this is enough)

d = i.sum(axis=2)
pylab.figure(figsize=(15,15))
pylab = imshow(d)
plt.axis('tight')
pylab.show()

This spectrum is given in pixel. But I would like to have this in the units of length. I will hope you may give me some advices.

Upvotes: 0

Views: 2303

Answers (2)

fjarri
fjarri

Reputation: 9726

Do you mean that you want axis ticks to show your custom dimensions instead of the number of pixels in d? If yes, use the extent keyword of imshow:

import numpy
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

d = numpy.random.normal(size=(20, 40))

fig = plt.figure()
s = fig.add_subplot(1, 1, 1)
s.imshow(d, extent=(0, 1, 0, 0.5), interpolation='none')
fig.tight_layout()
fig.savefig('tt.png')

enter image description here

Upvotes: 2

Greg
Greg

Reputation: 12234

I'm guess a bit at what your problem is, so let's start by stating my interpretation/ You have some 2D data d that you plot using imshow and the units on the x and y axes are in the number of pixels. For example in the following we see the x axis labelled from 0 -> 10 for the number of data points:

import numpy as np
import matplotlib.pyplot as plt

# Generate a fake d
x = np.linspace(-1, 1, 10)
y = np.linspace(-1, 1, 10)
X, Y = np.meshgrid(x, y)
d = np.sin(X**2 + Y**2)

plt.imshow(d)

enter image description here

If this correctly describes your issue, then the solution is to avoid using imshow, which is designed to plot images. Firstly this will help as imshow attemps to interpolate to give a smoother image (which may hide features in the spectrum) and second because it is an image, there is no meaningful x and y data so it doesn't plot it.

The best alternative would be to use plt.pcolormesh which generate a psuedocolor plot of a 2D array and takes as arguments X and Y, which are both 2D arrays of points to which the values of d correspond.

For example:

# Generate a fake d
x = np.linspace(-1, 1, 10)
y = np.linspace(-1, 1, 10)
X, Y = np.meshgrid(x, y)
d = np.sin(X**2 + Y**2)

plt.pcolormesh(X, Y, d)

enter image description here

Now the x and y values correspond to the values of X and Y.

Upvotes: 0

Related Questions