Search code examples
pythonmatplotlibplotmatlab-figureaxes

Figures different size although figsize() was used?


I am trying to create horizontal boxes (axes) with some markers.

Everything is working fine except that the size of the axes change for different ranges of the x axis, despite the y axis being adjusted proportionally (i.e., height variable uses the figsize ratio to calculate the appropriate y max value).

minimum = avg - 2 * std
maximum = avg + 2 * std
height = (2/8)*(maximum-minimum)

fig = plt.figure(figsize=(8, 2))
ax = fig.add_axes([minimum, 0, maximum, height])
ax.set_xlim((minimum, maximum))
ax.set_ylim((0,height))

Below is another variation of the function the code above is from, but the result is different for different values within a same function

minimum = 0
maximum = avg * 2
height = (2/8)*(maximum-minimum)

fig = plt.figure(figsize=(8, 2))
ax = fig.add_axes([minimum, 0, maximum, height])
ax.set_xlim((minimum, maximum))
ax.set_ylim((0,height))

screenshot of what the different graphs looks like There were a some questions regarding the use of figsize, but nothing that relates to my case.

Thanks in advance!


Solution

  • In the code above you deliberately create axes with different size using

    ax = fig.add_axes([minimum, 0, maximum, height])
    

    so it's no wonder they are different. As long as minimum, maximum and height are not always the same, you get different sized axes.

    Instead use

    ax = fig.add_subplot(111)