Search code examples
pythonmatplotlibfigure

Matplotlib box sizes are not equal


I have two plots with the same figsize and I want to get them to have the same box size.

Does anyone know how to achieve that?

Here is my code:

lstOfNames = []
fig,ax = plt.subplots(figsize=(24,12))
plt.plot(price.index, price.values)
plt.tick_params(axis='both', which='major', labelsize=18)
plt.setp(ax.get_xticklabels(), visible=False)


fig,ax = plt.subplots(figsize=(24,12))
for i in df_COT.columns:
    plt.plot(df_COT[i],linewidth=2)
    lstOfNames.append(i)

plt.axhline(y=0, color='black', linestyle='--')
plt.axhline(y=50000, color='black', linestyle=':')
plt.axhline(y=100000, color='black', linestyle=':')
plt.axhline(y=-50000, color='black', linestyle=':')
plt.axhline(y=-100000, color='black', linestyle=':')

plt.legend(lstOfNames,fontsize=17)
plt.title("COT Report - Non-Commercial (Large Speculators)", fontsize = 30)
plt.tick_params(axis='both', which='major', labelsize=20)#

fig.autofmt_xdate()

plt.savefig("COT Report.png", bbox_inches='tight')
plt.show()

The output I currently have:

Picture of the problem


Solution

  • Since, I don't have the data you are plotting, I couldn't check running your code: You can add

    f, (ax1, ax2) = plt.subplots(2,1,sharex=True,figsize=(24,12))
    

    and check.

    As for your comment, below is an working example:

    import numpy as np
    import matplotlib.pyplot as plt
    
    #Some dummy data
    x = np.linspace(0,10,1001)
    y = x+1
    
    
    fig, (ax1, ax2) = plt.subplots(2, sharex=True)
    
    ## if you want to control the spacing between the subpots
    ##fig, (ax1, ax2) = plt.subplots(2, sharex=True, gridspec_kw={'hspace': 0})
    
    fig.suptitle('Using sharex')
    ax1.plot(x, y)
    ax2.plot(x + 1, -y)
    plt.show()