Last active
September 30, 2020 13:28
-
-
Save ihsgnef/e093ae1d78f6d5d7678f597c612a0e20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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