in model_card_toolkit/utils/graphics.py [0:0]
def _draw_histogram(graph: _Graph) -> Optional[_Graph]:
"""Draw a histogram given the graph.
Args:
graph: The _Graph object represents the necessary data to draw a histogram.
Returns:
A _Graph object, or None if plotting raises TypeError given the raw data.
"""
if not graph:
return None
try:
# generate and open a new figure
figure, ax = plt.subplots()
# When graph.x or y is str, the histogram is ill-defined.
ax.barh(graph.y, graph.x, color=graph.color)
ax.set_title(graph.title)
if graph.xlabel:
ax.set_xlabel(graph.xlabel)
if graph.ylabel:
ax.set_ylabel(graph.ylabel)
for index, value in enumerate(graph.x):
show_value = f'{value:.2f}' if isinstance(value, float) else value
# To avoid the number has overlap with the box of the graph.
if value > 0.9 * max(graph.x):
ax.text(value - (value / 10), index, show_value, va='center', color='w')
else:
ax.text(value, index, show_value, va='center')
graph.figure = figure
graph.base64str = figure_to_base64str(figure)
except TypeError as e:
logging.info('skipping %s for histogram; plot error: %s:', graph.name, e)
return None
finally:
# closes the figure (to limit memory consumption)
plt.close()
return graph