2

I want to make square independent from the axis units. I know I can set the figure dimensions equal with figure(figsize=(10, 10)) for example, and I can set the axis scale ratio equal with set_aspect('equal'), but how can I force the actual axis length to be equal, e.g., make xaxis and yaxis each 10 inches long?

EDIT Example code

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MaxNLocator
import numpy as np

x1 = 1
y1 = [10., 1000.]
err1 = 0.00865
x2 = 2
y2 = [9., 900.]
err2 = 0.00658

len_xaxis,len_yaxis = 5.,5. #fix here your numbers
xspace, yspace = .9, .9 # change the size of the void border here.
x_fig,y_fig = len_xaxis / xspace, len_yaxis / yspace

for i in range(2):
    plt.clf()
    #    fig = plt.figure(figsize=(6, 6))
    fig = plt.figure(figsize=(x_fig,y_fig))
    plt.subplots_adjust(left=1-xspace, right = xspace, top=yspace, bottom = 1-yspace)
    gs = gridspec.GridSpec(3, 1)
    gs.update(hspace=0., wspace=0.)
    ax1 = plt.subplot(gs[0:2, 0])

    ax1.errorbar(x1, y1[i], yerr=err1)
    ax1.errorbar(x2, y2[i], yerr=err2)

    ax1.invert_yaxis()

    plt.setp(ax1.get_xticklabels(), visible=False)  # Remove x-labels between the plots
    plt.xlim(0, 3)

    ax2 = plt.subplot(gs[2, 0], sharex=ax1)

    nbins = len(ax1.get_yticklabels())
    ax1.yaxis.set_major_locator(MaxNLocator(nbins=8, prune='both'))
    nbins = len(ax2.get_yticklabels())
    ax2.yaxis.set_major_locator(MaxNLocator(nbins=6, prune='both'))

    plt.tight_layout()
    plt.savefig('prune_%d.png' % i)
    plt.close()

1 Answer 1

2

Play with the plt.subplots_adjust() function. Example for 5 inches instead of 10:

len_xaxis,len_yaxis = 5.,5. #fix here your numbers

xspace, yspace = .9, .9 # change the size of the void border here.

x_fig,y_fig = len_xaxis / xspace, len_yaxis / yspace
figure(figsize=(x_fig,y_fig))
plt.subplots_adjust(left=1-xspace, right = xspace, top=yspace, bottom = 1-yspace)
plot([1,2,3],[-1,3,5])
Sign up to request clarification or add additional context in comments.

6 Comments

Well, that does not work. Depending on the data (and therefore opn the length of the axis labels) in different plots I get the same hight in all plots but not the same width. The x and y axis don't have a fix inch length ratio.
I tried your code with the example I postet here. Depending on what y-values I use (i.e., how many digits the y tick labels have) the axis length ratio changes, also with your code added.
This is not an example to work on. I had a look at that code, and the solution worked, the 2 axes (top and bottom) together occupy exactly 5x5 inches. Please edit the question with an example of it failing.
Thanks, I included an example already with your suggestion in it. You are right, your code works. However, I use plt.tight_layout(), which seems to override the adjustment and I get differing axis length ratios in the two plots. Is there a way to circumvent that?
Not that i am award of. Basically that command changes the same parameters of plt.subplots_adjust() overriding it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.