Comet 2P/Encke's orbit.
Comet 2P/Encke's orbit.

Table of contents

Examples, references

Useful packages

General Notes

Print precision of Numpy arrays:
import numpy as np
a = np.arange(25).reshape((5, 5)) / np.pi
print np.array2string(a, precision=3)
UTF-8 encoding
At the top of the file:
# -*- coding: utf-8 -*-

Astronomy Notes

Convert your FITS image to a color image with the Python Imaging Library and a Matplotlib color table:
from import fits
from PIL import Image
from matplotlib import pyplot as mpl

# chose your color map from Matplotlib
cmap =
# load your image
im = fits.getdata('image.fits')
# apply image scaling here, the end product should be an image with
# values ranging from 0 to 255
im = ...
# convert to a PIL image, generate the RGB values, combine, then save
pim = Image.fromarray(im.astype(uint8))
r = pim.point(lambda x: cmap(x/255.0)[0] * 255)
g = pim.point(lambda x: cmap(x/255.0)[1] * 255)
b = pim.point(lambda x: cmap(x/255.0)[2] * 255)
pim = Image.merge("RGB", (r, g, b))'image.png')

Matplotlib Notes

Matplotlib defaults in ~/.config/matplotlib/matplotlibrc.
backend      : GTK3Agg : DejaVu Serif
text.usetex         : False
text.latex.unicode : True
mathtext.fontset : stix
xtick.major.size : 8
xtick.minor.size : 4
ytick.major.size : 8
ytick.minor.size : 4
legend.numpoints : 1
legend.fontsize : medium
figure.facecolor : white
figure.subplot.left : 0.1
figure.subplot.right : 0.96 : 0.96
image.interpolation : nearest
image.origin : lower
contour.negative_linestyle : solid
ps.papersize : auto
ps.usedistiller : ghostscript
Change fonts to sans-serif.
import matplotlib as mpl
mpl.rc('font', family='sans-serif')
mpl.rc('mathtext', fontset='dejavusans')
Place the sun or earth symbol in a plot
annotate('$\odot$', xy)
annotate('$\oplus$', xy)
Change outer margins of a figure of subplots
gcf().subplots_adjust(left=0.07, bottom=0.05, top=0.98, right=0.98)
Change subplot spacings
gcf().subplots_adjust(left=0.07, bottom=0.05, top=0.98, right=0.98)
Change subplot spacings
gcf().subplots_adjust(wspace=0.3, hspace=0.3)
Remove axis labels
Remove axes
setp(gca(), frame_on=False, xticks=[], yticks=[])
Change axes widths
setp(gca().spines.values(), linewidth=2)
Change axis tick lengths and widths
tick_params(which='major', width=2, length=10)
tick_params(which='minor', width=2, length=4)
Rename and rotate xtick labels
loc, lab = xticks(arange(len(labels)), labels)
setp(lab, rotation=45, ha='right')
Axis font sizes
setp(gca().get_ymajorticklabels(), fontsize='large')
labels = setp(gca(), ylabel='y axis', xlabel='x axis')
setp(labels, fontsize='large')
Legend font sizes
from matplotlib.font_manager import FontProperties
One point per legend item