pythonmatplotlibbox

Setting geometrical width/height for axes


I have trouble to produce four same sized plots.

I have four different plots, which are to be shown in a 2x2 matrix in a document. Two of the them have a second y-axis, and one of these have a slightly higher ax title (a greek letter). So, they come out in four differnt sizes of the plot, which does not look good. Additionally i Want to have them in single plots to give them an individual label.

Is there a way to directly set the length of the single axis in inch, so that they have exaclty the same size? And/or an option to define the origin ( in ccordinates) to prevent them from having a differnt adjustment?

Can I force them to be squred and equal using one plot? In this case, i will bite the bullet.

Thanks alot

Bad looking

This is how i plot each of the figures:

pre,ax = plt.subplots(figsize=(3,3))
ax2 = ax.twinx()
ax.plot([1,2],[3,4])
ax2.plot([3,4],[100,1000])


ax.set_box_aspect(1)
ax2.set_box_aspect(1)

plt.show()

Solution

  • To put an axes at exactly a given position in inches is relatively trivial. The following puts the axes exactly 0.5 inches from each side.

    
    import matplotlib.pyplot as plt
    w = 4
    h = 3
    margin = 0.5
    fig =plt.figure(figsize=(w, h), facecolor='lightblue')
    
    pos = [margin/w, margin/h, (w-2*margin)/ w, (h-2*margin)/h]
    
    ax = fig.add_axes(pos)
    
    plt.show()
    

    enter image description here

    This has been answered before, but many of the other solutions are pretty complex, whereas this is super straightforward.