[Python] Plot characters on a graph using matplotlib without spaces

Discussão em 'Python' iniciado por Stack, Julho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I want to plot some data, but the problem that I am facing is that I am unable to plot the data without spaces between them.

    The data should print out a letter like this:

    [​IMG]

    My code:

    import matplotlib.pyplot as plt
    import numpy as np

    data = [
    {'x': 0, 'char': '█', 'y': 0},
    {'x': 0, 'char': '█', 'y': 1},
    {'x': 0, 'char': '█', 'y': 2},
    {'x': 1, 'char': '▀', 'y': 1},
    {'x': 1, 'char': '▀', 'y': 2},
    {'x': 2, 'char': '▀', 'y': 1},
    {'x': 2, 'char': '▀', 'y': 2},
    {'x': 3, 'char': '▀', 'y': 2},
    ]
    # Create the plot
    fig, ax = plt.subplots()

    # Plot each character
    for item in data:
    ax.text(item['x'], item['y'], item['char'],
    fontsize=14, family='monospace', ha='center', va='bottom')

    # Compute axis limits
    x_vals = [d['x'] for d in data]
    y_vals = [d['y'] for d in data]
    ax.set_xlim(min(x_vals) - 0.5, max(x_vals) + 0.5)
    ax.set_ylim(min(y_vals) - 0.5, max(y_vals) + 0.5)

    # Remove padding and axes
    ax.axis('off')

    # # Remove margins
    plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
    plt.margins(0, 0)

    plt.show()


    The result of the code:

    [​IMG]

    How do I plot the points without the large spaces?

    Continue reading...

Compartilhe esta Página