Healpy Mollview function returned nothing - healpy

I am a new healpy user. I used healpy tutorial available at page http://healpy.readthedocs.org/en/latest/tutorial.html for creating a map but after execution of "healpy.mollview" command it returned nothing and no plot was visible. Need Help!
I have searched the problem already but unable to find the exact situation anywhere
Thanks,
Jibran

For using healpy plotting functions, the best is to use ipython, in particular:
ipython --pylab

You have to show the plot
import matplotlib.pyplot as plt
plt.show()
as you would do for any plot in matplotlib

Related

Different images in Image.show() and Image.save() in PIL

I generate a PIL image from a NumPy array. The image showed by show function differs from what is saved by the save function directly called after show. Why might that be the case? How can I solve this issue? I use TIFF file format. Viewing both images in Windows Photos App.
from PIL import Image
import numpy as np
orig_img = Image.open('img.tif'))
dent = Image.open('mask.tif')
img_np = np.asarray(orig_img)
dent_np = np.asarray(dent)
dented = img_np*0.5 + dent_np*0.5
im = Image.fromarray(dented)
im.show('dented')
im.save("dented_2.tif", "TIFF")
Edit: I figured out that the save function saves correctly if the values for pixel in the NumPy array called 'dented' are normalized to 0,1 range. However then show function shows the image completely black.
I suspect the problem is related to the dtype of your variable dented. Try:
print(img_np.dtype, dented.dtype)
As a possible solution, you could use:
im = Image.fromarray(dented.astype(np.uint8))
You don't actually need to go to Numpy to do the maths and then convert back if you want the mean of two images, because you can do that with PIL.
from PIL import ImageChops
mean = ImageChops.add(imA, imB, scale=2.0)

Export PDF: font not found

I am trying to export my figure as a PDF. I run into the problem that when I want to edit it (in this case by Graphic on macOS) that the font appears not to be found by the editor. My question is, how do I solve this? Can I 'install' the fonts used by matplotlib?
Example
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([0,1], [0,1])
plt.savefig('test.pdf')
This PDF looks fine in Preview:
But in the editor, it gives gibberish:
Failing solution
Setting
matplotlib.rc("pdf", fonttype=42)
(see this answer).
Partly working solution
What works is to install all matplotlib's fonts. I have followed this answer to find all matplotlib's ttfs, and installed them. This works and solves the problem.
But... This does solve the issues when LaTeX is enabled, by
import matplotlib
matplotlib.rcParams['text.usetex'] = True
How do I install the fonts that matplotlib uses now?
A solution here is to export as SVG. However, for some reason this takes ages on my system (see this bug).
I have found a workaround. This is to convert all fonts to outlines before editing. One way to do this is with GhostScript with the option -dNoOutputFonts, as described in this answer:
gs -o file-with-outlines.pdf -dNoOutputFonts -sDEVICE=pdfwrite file.pdf
What pdf editor are you using? I don't know how to check whether an attempt to solve this is working... but I always use this code to save images in pdf or png:
plt.savefig('test.pdf', edgecolor='none', bbox_inches="tight")

The exponential distribution didn't show up in the shell

Here is a short code :
import numpy as np
from scipy.stats import expon
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.001)
plt.plot(x, expon.pdf(x))
When I type that in the shell in using ipython, that didn't show up anything while it is supposed to plot the exponential distribution for x between 0 and 10. Could anyone be able to tell me what is the problem here?
I got that, but the plot did show up
In [16]: plt.plot(x, norm.pdf(x))
Out[16]: [<matplotlib.lines.Line2D at 0x7fbfe6bc2890>]
Thanks in advance!
P.S. Be aware that I am using Ubuntu 16.10 (Linux distribution).
It seems you are using IPython. Depending on in which environment you use it, you can create inline plots or plot to a window.
Ipython in Jupyther QtConsole allows to set the inline backend, %matplotlib inline.
In IPython without graphical support, you need to invoke a plotting window using plt.show()

Inline display when using GalSim on iPython Notebook

It seems that all the demos write the image file to the system so that we can view it with DS9. Is there a way we can view the images we make inline on iPython Notebook, as we code? This would speed up my coding a lot.
Thank you!
GalSim Image objects store the pixel values in a numpy array: im.array. So if you're using an iPython notebook with %matplotlib inline, you can just do plt.imshow(im.array) to display an image of the data on the screen.

Python 3.4: Where is the image library?

I am trying to display images with only builtin functions, and there are plenty of Tkinter examples online. However, none of the libraries work:
import Image # none of these exist.
import tkinter.Image
import _tkinter.Image
etc
However, tkinter does exist, a hellow-world with buttons worked fine.
I am on a MacBook pro 10.6.8 and using PyCharm.
Edit: The best way so far (a little slow but tolerable):
Get the pixel array as a 2D list (you can use a third-party .py to load your image).
Now you make a data array from the pixels like this (this is the weirdest format I have seen, why not a simple 2D array?). This may be sideways, so you may get an error for non-square images. I will have to check.
Imports:
from tkinter import *
import tkinter
data = list() # the image is x pixels by y pixels.
y = len(pixels)
x = len(pixels[0])
for i in range(y):
col_str.append('{')
for j in range(x):
data.append(pixels[i][j]+" ")
data.append("} ")
data = "".join(data)
Now you can create an image and use put:
# PhotoImage is builtin (tkinter).
# It does NOT need PIL, Pillow, or any other externals.
im = PhotoImage(width=x, height=y)
im.put(col_str)
Finally, attach it to the canvas:
canvas = tkinter.Canvas(width=x, height=y)
canvas.create_image(x/2, y/2, image=GLOBAL_IMAGE) # x/2 and y/2 are the center.
tK.mainloop() # enter the main loop and it will be drawn.
Image must be global or else it may not show up because the garbage collector gets greedy.
PIL hasn't been updated since 2009, with Python 3 support being terminally stuck at "later."
Instead, try pillow, which has forked PIL and provides Python 3 support.

Resources