Search code examples
pythonmatplotlibsubplot

matplotlib subplots: how to freeze x and y axis?


Good evening

matplotlib changes the scaling of the diagram when drawing with e.g. hist() or plot(), which is usually great.

Is it possible to freeze the x and y axes in a subplot after drawing, so that further drawing commands do not change them anymore? For example:

fig, (plt1, plt2) = plt.subplots(2, 1, figsize=(20, 10))
plt1.hist(…)
plt1.plot(…)

# How can this get done?:
plt1.Freeze X- and Y-Axis

# Those commands no longer changes the x- and y-axis
plt1.plot(…)
plt1.plot(…)

Thanks a lot, kind regards, Thomas


Solution

  • The answer of @jfaccioni is almost perfect (thanks a lot!), but it does not work with matplotlib subplots (as asked) because Python, as unfortunately so often, does not have uniform attributes and methods (not even in the same module), and so the matplotlib interface to a plot and a subplot is different. In this example, this code works with a plot but not with a subplot:

    # this works for plots:
    xlims = plt.xlim()
    # and this must be used for subplots :-(
    xlims = plt1.get_xlim()
    

    therefore, this code works with subplots:

    import matplotlib.pyplot as plt
    
    fig, (plt1, plt2) = plt.subplots(2, 1, figsize=(20, 10))
    
    # initial data
    x = [1, 2, 3, 4, 5]
    y = [2, 4, 8, 16, 32]
    plt1.plot(x, y)
    
    # Save the current limits here
    xlims = plt1.get_xlim()
    ylims = plt1.get_ylim()
    
    # additional data (will change the limits)
    new_x = [-10, 100]
    new_y = [2, 2]
    plt1.plot(new_x, new_y)
    
    # Then set the old limits as the current limits here
    plt1.set_xlim(xlims)
    plt1.set_ylim(ylims)
    
    plt.show()
    

    btw: Freezing the x- and y axes can even be done by 2 lines because once again, python unfortunately has inconsistent attributes:

    # Freeze the x- and y axes:
    plt1.set_xlim(plt1.get_xlim())
    plt1.set_ylim(plt1.get_ylim())
    

    It does not make sense at all to set xlim to the value it already has. But because Python matplotlib misuses the xlim/ylim attribute and sets the current plot size (and not the limits!), therefore this code works not as expected.

    It helps to solve the task in question, but those concepts makes using matplotlib hard and reading matplotlib code is annoying because one must know hidden / unexpected internal behaviors.