Hauptsatz.mp3
Hauptsatz.mp3

Reputation: 47

Scaling graphic without scaling labels, text and axis ticks etc

I have a problem with the scaling of my graphics. I use imshow to plot two matrices the first being a 2x2 matrix and the second a 5x5 matrix. I now what both to have the same size of the boxes representing the entries of the matrices. But I want them to be same in absolute size (like pixels). If i plot both and compare them clearly the 2x2 matrix boxes are much bigger relative to the numbers inside than the 5x5 matrix. 2x2 matrix, too big boxes 5x5 matrix, right box size I tried to use the "figsize" parameter of the plt.figure() function but this also rescales the numbers in the boxes. Another thing I tried is the "extent" parameter of imshow, which did not work if i try to just make the boxes smaller. It just scaled them back up. (it works tho if i make the bounding box wider, then it automatically makes them thinner but that's not what i want, example below). with use of extent: wider and thinner but I don't what that

Now again: I kind of want to resize the boxes but don't change the size of the text/numbers so that it does not look dump if i put the graphics right next to each other in an article. It does not have to be a way to automatically match the two figure box sizes, I'm already happy with any way to resize the boxes, because it does not have to be 100% accurate. Anyone has an idea how i can do this ? Thanks a lot already!!

Here is the code for the two graphics with quadratic boxes (what i want, but just changed sizes):

import matplotlib.pyplot as plt
import numpy as np
plt.style.use("seaborn-dark")

def gfx_1():
    fig = plt.figure()
    ax1 = plt.subplot(111)
    data = [[1, 2], [3, 4]]
    ax1.imshow(data, interpolation="nearest")
    for (i, j), data in np.ndenumerate(data):
        ax1.text(i, j, s=str(data), ha='center', va='center')

    ax1.set_xticklabels([])
    ax1.set_yticklabels([])
    ax1.set_xticks(np.arange(-0.5, 1.5, 1.))
    ax1.set_yticks(np.arange(-0.5, 1.5, 1.))
    ax1.grid(linewidth=2)

    plt.savefig("2x2.png")


def gfx_2():
    fig = plt.figure()
    ax1 = plt.subplot(111)
    data = [[1, 2, 3, 4, 5], [3, 4, 5, 6, 7], [6, 7, 8, 9, 10], [9, 10, 11, 12, 13], [12, 13, 14, 15, 16]]
    ax1.imshow(data, interpolation="nearest")
    for (i, j), data in np.ndenumerate(data):
        ax1.text(i, j, s=str(data), ha='center', va='center')

    ax1.set_xticklabels([])
    ax1.set_yticklabels([])
    ax1.set_xticks(np.arange(-0.5, 4.5, 1.))
    ax1.set_yticks(np.arange(-0.5, 4.5, 1.))
    ax1.grid(linewidth=2)

    plt.savefig("5x5.png")

and the modified one with extend (which i don't what):

def gfx_1():
    fig = plt.figure()
    ax1 = plt.subplot(111)
    data = [[1, 2], [3, 4]]
    ax1.imshow(data, interpolation="nearest", extent=(-0.5, 3.5, -0.5, 1.5))
    for (i, j), data in np.ndenumerate(data):
        ax1.text(i*2, j, s=str(data), ha='center', va='center')

    ax1.set_xticklabels([])
    ax1.set_yticklabels([])
    ax1.set_xticks(np.arange(-0.5, 3.5, 2.))
    ax1.set_yticks(np.arange(-0.5, 1.5, 1.))
    ax1.grid(linewidth=2)

    plt.savefig("2x2_wide.png")

Upvotes: 2

Views: 55

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40707

I think you already found the correct answer by using figsize. Sure, the resulting image might look bigger with the 2x2 grid, but it's probably just a matter of zoom in your image visualization program. If you were to show them side by side at their native resolution, the squares would look the same size.

In other words, if you create a 2x2 grid in a 2 inches x 2 inches image, then each box would be a little smaller than 1 inch wide (because of the axes and everything else). If you create your 5x5 grid in a 5x5 inches images, then the boxes would still be roughly 1 inch wide

Here is created the two images with the below code, and copy-pasted them side by side in an image editor: enter image description here

def gfx_1():
    fig = plt.figure(figsize=(2,2))
    ax1 = plt.subplot(111)
    data = [[1, 2], [3, 4]]
    ax1.imshow(data, interpolation="nearest")
    for (i, j), data in np.ndenumerate(data):
        ax1.text(i, j, s=str(data), ha='center', va='center')

    ax1.set_xticklabels([])
    ax1.set_yticklabels([])
    ax1.set_xticks(np.arange(-0.5, 1.5, 1.))
    ax1.set_yticks(np.arange(-0.5, 1.5, 1.))
    ax1.grid(linewidth=2)

    plt.savefig("./2x2.png")


def gfx_2():
    fig = plt.figure(figsize=(5,5))
    ax1 = plt.subplot(111)
    data = [[1, 2, 3, 4, 5], [3, 4, 5, 6, 7], [6, 7, 8, 9, 10], [9, 10, 11, 12, 13], [12, 13, 14, 15, 16]]
    ax1.imshow(data, interpolation="nearest")
    for (i, j), data in np.ndenumerate(data):
        ax1.text(i, j, s=str(data), ha='center', va='center')

    ax1.set_xticklabels([])
    ax1.set_yticklabels([])
    ax1.set_xticks(np.arange(-0.5, 4.5, 1.))
    ax1.set_yticks(np.arange(-0.5, 4.5, 1.))
    ax1.grid(linewidth=2)

    plt.savefig("./5x5.png")

gfx_1()
gfx_2()

Upvotes: 1

Related Questions