Saturday, September 27, 2014

Code formatting in Blogger

I found this answer on stackoverflow about how to format source code in blogs on blogger, which pointed this tutorial with detailed instructions. It worked for me with Python! Many Thanks to David Craft and Alex Gorbatchev.

Also note the useful tip here about loading only the format styles that you need for your blog, to speed up page load.

How-to (for Python)

Add the following to Blogger template above the </head> tag.







In the blog editor, switch to HTML mode and insert code within <pre> as follows.

You can find more brushes here.

A matplotlib example

The following matplotlib script shows some cases where the matplotlib defaults were not sufficient for the job, and how to customize the relevant properties. The main tweaks were:
  • changing the font type from Type 3 to True Type
  • scaling the y values
  • using latex syntax in the labels
  • changing the fontsize for axis labels, axis ticks, and legends
  • two legends
  • using proxy objects (lines or patches) for the legends
  • using axis coordinates for locating the legend
  • semi-transparent legends and grid lines
Here is the python code and the resulting figure:

import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.patches as mpatches
import numpy as np
import json
import sys
import os

# change font type to True Type to avoid Type 3 fonts
# (which are not allowed by some conferences)

mpl.rcParams['pdf.fonttype'] = 42


# x = 1-d array with x values
# ys = six 1-d arrays with y values (one for each line we want to plot).
#      Of these 3 are of one kind, and the other 3 are of another kind.
# xlabel, ylabel = labels for the x and y axis

x, ys, xlabel, ylabel = get_plot_info(datafile)


# I want to scale the y values so that the y axis is easier to read.

ys = ys / 10   # ys is a numpy array


# The plot will be shrunk when it is included in the paper, so that the 
# default fontsize becomes too small. Select a larger fontsize globally.

fontsize = 30


# Plot the first three lines in blue with different line styles;
# then plot the other three in green with similar line styles.

plt.plot(x, ys[0], 'b-', lw=3)
plt.plot(x, ys[1], 'b--', lw=3)
plt.plot(x, ys[2], 'b:', lw=3)
plt.plot(x, ys[3], 'g-', lw=3)
plt.plot(x, ys[4], 'g--', lw=3)
plt.plot(x, ys[5], 'g:', lw=3)


# Set axis labels with larger fontsize.
# Mention the scaling done to the y values.

plt.xlabel(xlabel, fontsize=fontsize)
plt.ylabel(ylabel + r' $\div 10$', fontsize=fontsize)  # latex syntax works!


# Increase the fontsize of the axis ticks

ax = plt.gca()
for labx in ax.get_xticklabels():
    labx.set_fontsize(fontsize)
for laby in ax.get_yticklabels():
    laby.set_fontsize(fontsize)


# If you want the lines to reach the left and right extremities of the graph,
# reset the x-limits.
ax.set_xlim( (min(x), max(x)) )

# Instead of showing 6 entries in the legend (one for each of the 6 lines),
# we show one legend for the color code, and another for the line style.
# ('MCTM-...' are methods, and 'en' etc. are languages for which we ran
# the method.)

# first legend (for color code)

blue_patch = mpatches.Patch(color='blue')
green_patch = mpatches.Patch(color='green')

# loc=(x,y) are the coordinates of the lower left corner of the legend,
# where (0,0) if the lower left corner of the axes, and (1,1) is the
# upper right corner.
# framealpha is the transparency level (0=transparent, 1=opaque).

leg1 = plt.legend((blue_patch, green_patch), ('MCTM-DSGNP','MCTM-D'), 
                  loc=(.3,.6), fontsize=fontsize, framealpha=.5)
plt.gca().add_artist(leg1)

# second legend (for line style)

# plot empty arrays in black (to avoid the colors used above).

l_en, = plt.plot([], [], 'k-', lw=3, label='en')
l_hi, = plt.plot([], [], 'k--', lw=3, label='hi')
l_hir, = plt.plot([], [], 'k:', lw=3, label='hir')
plt.legend((l_en, l_hi, l_hir), ('en','hi', 'hir'), 
                  loc='lower right', fontsize=fontsize, framealpha=.5)

# Add grid lines, but make them semi-transparent (otherwise they appear too
# prominent when the figure is shrunk).

plt.grid(alpha=.5)


# The large fontsize pushes axis labels out of the figure; but matplotlib
# offers a function to auto-correct this.

plt.tight_layout()


# The saving format is chosen automatically based on the file extension.
plt.savefig(figname+'.pdf')