Skip to content

Instantly share code, notes, and snippets.

@ihsgnef
Last active September 30, 2020 13:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ihsgnef/e093ae1d78f6d5d7678f597c612a0e20 to your computer and use it in GitHub Desktop.
Save ihsgnef/e093ae1d78f6d5d7678f597c612a0e20 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
color_name = 'color{}'
define_color = '\definecolor{{{}}}{{HTML}}{{{}}}'
box = '\\mybox{{{}}}{{\strut{{{}}}}}'
cmap = plt.cm.get_cmap('RdBu')
for i, x in enumerate(np.arange(0, 1, 0.1)):
rgb = matplotlib.colors.rgb2hex(cmap(x)[:3])
print(define_color.format(color_name.format(i), rgb[1:]))
print()
print('''\\newcommand*{\mybox}[2]{\\tikz[anchor=base,baseline=0pt] \\node[fill=#1!60!white] (X) {#2};}''')
print()
def latex_colorize(text, weights):
s = ''
for w, x in zip(text, weights):
color = np.digitize(x, np.arange(0, 1, 0.1)) - 1
s += ' ' + box.format(color_name.format(color), w)
return s
text = "What company won a free advertisement due to the QuickBooks contest ?".split()
weights = [0.1, 0.0, 0.0, 0.8, 0.9, 0.4, 0.2, 0.2, 0.1, 0.4, 0.5, 0.6]
assert len(text) == len(weights)
print('\\footnotesize', latex_colorize(text, weights))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment