Fundamentals 7 min read

Overlay Sequential Guess Results in Matplotlib Without New Figures

Learn how to overlay successive word‑guess visualisations in a single Matplotlib figure by reusing the figure manager, colour‑coding letters for correctness, and handling Jupyter‑specific output clearing, enabling a Wordle‑style game without generating multiple plots.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Overlay Sequential Guess Results in Matplotlib Without New Figures

S Series: Adding Legend to Existing Matplotlib Figure

This article demonstrates how to display successive word‑guess results in a single Matplotlib figure, using color‑coded boxes to indicate correct, misplaced, extra, or absent letters.

Environment

Windows 10

Python 3.8

Matplotlib 3.3.4

Goal

Implement a Wordle‑style guessing program where each guess is visualised with green (correct position), yellow (correct letter wrong position), gray (letter not in target) and white (extra letters) backgrounds, and each new guess is drawn below the previous one.

First Approach (no result retention)

from collections import Counter
from itertools import zip_longest
import matplotlib.pyplot as plt
from IPython.display import clear_output

def show_string(fig, x=0.3, y=0.8, string_list=None):
    # x: horizontal coordinate, y: vertical coordinate
    clear_output()
    if not string_list:
        return fig
    for s, fc in string_list:
        bbox_props = dict(boxstyle="round", fc=fc, ec="0.5", alpha=0.5)
        plt.text(x, y, s, size=14, bbox=bbox_props)
        x += 0.1
    return fig

def word_color(pat, repl):
    # assign a colour to each letter
    pat_count = Counter(pat)
    for x, y in zip_longest(pat, repl):
        if not (x and y):
            yield (y, 'white')
        elif x == y:
            pat_count[y] -= 1
            yield (y, 'green')
        elif y in pat and pat_count.get(y, 0) > 0:
            yield (y, 'yellow')
        else:
            yield (y, 'gray')

if __name__ == '__main__':
    fig = plt.figure()
    answer = 'world'  # target word
    y = 0.8
    while True:
        result = input('Enter your guess: ')
        fig = show_string(fig, y=y, string_list=word_color(answer, result))
        y -= 0.2
        plt.show()
        if result == answer:
            print('Correct guess!')
            break

The code uses Counter to count letter frequencies and zip_longest to iterate over the target and guess strings, filling missing positions with None.

Running the script in Jupyter produces two independent figures, because after plt.show() the figure object is cleared.

Overlaying Results on the Same Figure

To keep the previous drawing, the current figure manager is retrieved and the existing fig object is reassigned before drawing the next guess.

def show_string(fig, x=0.3, y=0.8, string_list=None):
    clear_output()
    if not string_list:
        return fig
    # --- use figure manager
    fm = plt.get_current_fig_manager()
    fm.canvas.figure = fig
    fig.canvas = fm.canvas
    # ------------------
    for s, fc in string_list:
        bbox_props = dict(boxstyle="round", fc=fc, ec="0.5", alpha=0.5)
        plt.text(x, y, s, size=14, bbox=bbox_props)
        x += 0.1
    return fig

With this modification, each new guess appears on the previous plot, as shown in the following screenshots.

Conclusion

The example runs in a Jupyter notebook; for other environments, comment out or remove the clear_output() call. This short demonstration shows how to overlay subsequent visualisations on an existing Matplotlib figure, avoiding the creation of multiple separate plots.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonvisualizationwordlelegend
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.