I wanted to plot this image in fits format.
But I get this instead
I've tried to change the axes with imshow but it doesn't do anything better. This is the code that i'm using.
import matplotlib.pyplot as plt
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)
from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits
image_file = get_pkg_data_filename('Or.fits')
fits.info(image_file)
image_data = fits.getdata(image_file, ext=0)
print(image_data.shape)
plt.figure()
plt.imshow(image_data, cmap='gray')
plt.colorbar()
From the second image that you posted in your question, it seems that image_data has super large values with most the data points being very small. Try mapping data points logarithmically to hopefully solve the problem.
Add the below to the beginning of your code:
from matplotlib.colors import LogNorm
And change your plotting line to the following so that matplotlib uses the logarithm of those values when plotting your image.
plt.imshow(image_data, cmap='gray', norm=LogNorm())
Related
I am trying to plot a scatter graphic over an image. The problem is that the image is in logarithmic scale, so I need to change the scale of the scatter to a determined one. But when I do that, the image changes aswell, when I don't want it to change.
Code:
import pandas as pd
import matplotlib.pyplot as plt
import math
table = pd.read_csv('Coef_Data.csv', sep = ',', header = 0)
img = plt.imread("img.png")
plt.figure(1)
plt.scatter(table['Fcoef'], table['Vcoef'], zorder=1)
plt.imshow(img,zorder=0)
plt.imshow(img, extent=[0, 1e6, 0, 1e6])
plt.ylabel('Vcoef')
plt.xlabel('Fcoef')
Plot with the image over the graphic with the scale in a wrong way
The real scale is logarithmic (from 10^0 to 10^6)
when I try this, the image turns into a scribble:
plt.yscale('log')
plt.xscale('log')
Just trying to change the scale of the plot without changing the image.
Thanks
I'm attempting to open up an astronomical image and plot it as a figure, which is done by my code, however, I am trying to enlarge a section of the fits file as a subplot on the same figure.
import matplotlib.pyplot as plt
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)
from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits
image_file = get_pkg_data_filename('814wmos.fits')
image_data = fits.getdata('814wmos.fits', ext=0)
plt.figure()
plt.imshow(image_data, cmap='gray')
This gives me my fits file, and plots it, which without 'zooming' in on the coordinates[350:450,500:600] shows/ tells me very little. How can I plot this area as a subplot next to original image, of the same size (zoomed in for same axes)?
I have played around a bit and can't get saving a plot rendered with seaborn correctly. When using plt.savefig I lose the grid. However, using plt.show and then saving the figure manually works. This happens with eps and png as well. I need to render large amount of plots so this is a problem.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style = 'darkgrid', font_scale=2)
t = np.arange(100)
y = np.random.rand(len(t))
plt.plot(t,y)
plt.title('Test title')
plt.xlabel('Test xlab')
plt.ylabel('Tex $y_i = w_i x_i$')
plt.tight_layout()
#plt.show()
plt.savefig('test_plot.eps', format='eps')
Automatic save
Manual save
The solution was I had "savefig.transparent : True" in my matplotlibrc that I for some reason needed before. Changing this to False solved the problem in my case.
I am plotting a 2D data array with imshow in matplotlib. I have a problem trying to scale the resulting plot. The size of the array is 30x1295 points, but the extent in units are:
extent = [-130,130,0,77]
If I plot the array without the extent, I get the right plot, but if I use extent, I get this plot with the wrong aspect.
It is a pretty beginner question, but there is always a first time: How I can control the aspect and the size of the plot at the same time?
Thanks,
Alex
P.D. The code is, for the right case:
imshow(np.log10(psirhoz+1e-5),origin='lower')
and for the wrong one:
imshow(np.log10(psirhoz+1e-5),origin='lower',
extent =[z_ax.min(),z_ax.max(),rho_ax.min(),rho_ax.max()])
I hope this clarify a bit things.
I'm guessing that you're wanting "square" pixels in the final plot?
For example, if we plot random data similar to yours:
import numpy as np
import matplotlib.pyplot as plt
data = np.random.random((30, 1295))
fig, ax = plt.subplots()
ax.imshow(data, extent=[-130,130,0,77])
plt.show()
We'll get an image with "stretched" pixels:
So, first off, "aspect" in matplotlib refers to the aspect in data coordinates. This means we have to jump through a couple of hoops to get what you want.
import numpy as np
import matplotlib.pyplot as plt
def main():
shape = (30, 1295)
extent = [-130,130,0,77]
data = np.random.random(shape)
fig, ax = plt.subplots()
ax.imshow(data, extent=extent, aspect=calculate_aspect(shape, extent))
plt.show()
def calculate_aspect(shape, extent):
dx = (extent[1] - extent[0]) / float(shape[1])
dy = (extent[3] - extent[2]) / float(shape[0])
return dx / dy
main()
In this case, pyplot.matshow() might also be useful:
from matplotlib import pyplot as plt
import numpy as np
dat = np.array(range(9)).reshape(3,3)
plt.matshow(dat)
plt.show()
result:
I've got an image, and a measure associated with each column of its pixels. I'm using pyplot to create a figure with the image on top, and a plot of the column measurements below. I'm using something like this:
import numpy as np
import matplotlib.pyplot as plt
A = np.random.rand(34*52).reshape(34,52)
means = np.average(A,axis=0)
plt.figure()
plt.subplot(2,1,1)
plt.imshow(A, interpolation='nearest' )
plt.subplot(2,1,2)
plt.plot(means)
plt.show()
How can I stretch the image's width to the match that of the plots. That way, when looking at the measurements in the plot, the souce pixels will be in a column directly above it.
Turns out that it's as simple as giving aspect='auto' to the imshow call.
plt.imshow(A, interpolation='nearest', aspect='auto')