0

I am plotting two different graphs with matplotlib. The two plots are separate and must be kept that way since I am saving them onto file and have different uses for them.

As it can be seen on the picture below they are misaligned because the ylabels on the top graph are occupying more space than the one on the bottom. Is there a way to fix the width occupied by the labels? I have tried:

ax.set_ylabel('Y Axis Label', labelpad=50)

But it didn't do much

Importantly, I cannot change the label format (scientific or other).

enter image description here

the whole function that produces the single graph follows:

def prepare_plot(inputDataFrame : pd.DataFrame(), median_columns : list(str()), upper_lower_quantiles : int, labels : list(str())) :
    """
    inputDataFrame: dataframe with values
    median_columns: names of the columns to be used for median values
    upper_lower_quantiles: Delta (1, 2 or 3) compared to the median, if invalid input will default to 1
    labels: labels to be set
    """

    if upper_lower_quantiles not in [1,2,3] : upper_lower_quantiles = 1

    flike = io.BytesIO()

    df = inputDataFrame
    x   = df.index

    fig, ax = plt.subplots()
    fig.set_size_inches(15, 5)

    colors = ['g','r','b']

    for median, label, color in zip(median_columns,labels, colors):

        lower = median[:-1] + f'{3 - upper_lower_quantiles}'
        upper = median[:-1] + f'{3 + upper_lower_quantiles}'
        
        try:
            ax.plot(x, df[median], label=label, color=color)
            ax.fill_between(x,
                            df[lower], #Lower Bound
                            df[upper], #Upper Bound
                            color=color,
                            alpha=.1
                            )
        except:
            print(f'Error on Key ({median}), label ({label}), color ({color})')
            print(f'Not graphing the line')

    ax.grid()
    lgd = ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5), fontsize=10)
    
    fig.savefig(flike, bbox_extra_artists=(lgd,), bbox_inches='tight')
    b64 = base64.b64encode(flike.getvalue()).decode()
    
    #Saving in a format that is embeddable in  different other platforms
    return b64
Wing
  • 642
  • 2
  • 5
  • 16
  • Does this answer your question? [How to add padding to a plot in python?](https://stackoverflow.com/questions/42281851/how-to-add-padding-to-a-plot-in-python) – oskros Jan 31 '23 at 08:26
  • I tried both suggestions but it didn't do any good :-( The plots remained identical – Wing Jan 31 '23 at 09:01
  • Put the plots into subplots and save them separately, as per this question: https://stackoverflow.com/questions/4325733/save-a-subplot-in-matplotlib – Andrey Sobolev Jan 31 '23 at 09:02

0 Answers0