Small fix to alignement
This commit is contained in:
+17
-16
@@ -225,24 +225,26 @@ import matplotlib.pyplot as plt
|
||||
import matplotlib.colors as mcolors
|
||||
from highlight_text import ax_text
|
||||
|
||||
def render_score_text(label, score, error, width_px, fontsize=18,
|
||||
bg_color=(255, 255, 255, 255),
|
||||
with_error=True):
|
||||
# 1. Calculate Color Gradient
|
||||
def color(score):
|
||||
t = max(0.0, min(1.0, float(score) / 4.0))
|
||||
t = t*1.5 - 0.25
|
||||
t = max(0.0, min(1.0, t))
|
||||
red = 200 * (1 - t)
|
||||
green = 150 * t
|
||||
hex_color = mcolors.to_hex((red/255, green/255, 0))
|
||||
return mcolors.to_hex((red/255, green/255, 0))
|
||||
|
||||
def render_score_text(label, score, error, width_px, fontsize=18,
|
||||
bg_color=(255, 255, 255, 255),
|
||||
with_error=True):
|
||||
# 1. Calculate Color Gradient
|
||||
|
||||
# 2. Build highlight-text String & Properties
|
||||
# Wrap colored parts in < >
|
||||
score_str = f"{label} ; Note : <{score}>"
|
||||
hl_props = [{"color": hex_color, "fontweight": "bold"}]
|
||||
score_str = f"{label} Note : <{score}>"
|
||||
hl_props = [{"color": color(score), "fontweight": "bold"}]
|
||||
|
||||
if error and error != "null" and with_error:
|
||||
score_str += f" | <Error: {error}>"
|
||||
score_str += f" <{error}>"
|
||||
hl_props.append({"color": "orange", "fontweight": "bold"})
|
||||
|
||||
# 3. Wrap Text
|
||||
@@ -250,19 +252,16 @@ def render_score_text(label, score, error, width_px, fontsize=18,
|
||||
fig_width = width_px / dpi
|
||||
chars_per_line = int(fig_width * 10)
|
||||
|
||||
# Note: Since there is no LaTeX anymore, standard textwrap works well here
|
||||
import textwrap
|
||||
wrapped_text = textwrap.fill(score_str, width=chars_per_line)
|
||||
# wrapped_text = textwrap.fill(score_str, width=chars_per_line)
|
||||
|
||||
# 4. Render using highlight_text.ax_text
|
||||
num_lines = wrapped_text.count('\n') + 1
|
||||
fig_height = num_lines * 0.4 + 0.2
|
||||
# fig_height = 0.4 + 0.2
|
||||
fig_height = 0.8
|
||||
|
||||
fig, ax = plt.subplots(figsize=(fig_width, fig_height), dpi=dpi)
|
||||
ax.axis('off')
|
||||
|
||||
# Replaces plt.text
|
||||
ax_text(0.01, 0.95, wrapped_text,
|
||||
ax_text(0.02, 0.98, score_str,
|
||||
fontsize=fontsize,
|
||||
verticalalignment='top',
|
||||
horizontalalignment='left',
|
||||
@@ -270,7 +269,9 @@ def render_score_text(label, score, error, width_px, fontsize=18,
|
||||
ax=ax)
|
||||
|
||||
buf = io.BytesIO()
|
||||
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0.1, transparent=True)
|
||||
# Issues with tight bbox_inches.
|
||||
# plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0.05, transparent=True)
|
||||
plt.savefig(buf, format='png', pad_inches=0.05, transparent=True)
|
||||
plt.close(fig)
|
||||
buf.seek(0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user