Search code examples
pythonsubplotatlas-plot

Adding multiple Pull Plots with matplot lib


I was working in a plot that had a pull plot, which I defined as :

fig, (ax1, ax2) = aplt.ratio_plot(name=  canvasArray[kl], figsize=(800, 800), hspace=0.05)

enter image description here

and it was working fine, but now I have the need to add another pull plot in the image, so i tried:

fig, (ax1, ax2,ax3) = aplt.subplots(3,1,name="fig1", figsize=(850, 950))

and i got the resulting plot:

enter image description here

I tried some options like .set_aspect() but i keep getting the error AttributeError: 'Axes' object has no attribute 'set_aspect'. I would like that the main plot ocupy 2/4 of the full plot, and the pull plots 1/2 each, but i am having dificulties with that.

I am working in a object oriented enviroment, so i dont know if that changes things. I am using the Atlasplots package which uses matplotlib syntax. https://atlas-plots.readthedocs.io/en/latest/


Solution

  • I had an idea. Matplotlib.pyplot has a collection of parameters, and one of them controls the size of the plots. It is called: rcParams. This attribute internally is a dictionary that contains a lot of configurations. Take a look:

    >>> from matplotlib import pyplot as plt
    >>> plt.rcParams
    

    If you run the above lines of code you get the following. Yeah, those are a lot of things, but we have one specific key that may solve our problem. It is "figure.figsize", if you select this parameter like this:

    >>> plt.rcParams["figure.figsize"] = [6.0, 4.0]
    

    You can customize the plot sizes. So I think you would be able to use this at certain locations in your code and reset it to the default values when needed.

    To see what are the default values, just run this to get the output based on the key "figure.figsize":

    >>> plt.rcParams["figure.figsize"]
    
    [out]: ["your default", "values here"]
    

    Update: October 1, 2021

    I've just remembered that you can also unpack subplots (matplotlib.pyplot.subplots) and select directly the parameters, like this:

    >>> fig, ax = plt.subplots(figsize=("the size", "you want"))
    

    I've also noticed something very interesting. If you use rcParams["figure.figsize"] to control plot size, it will be persistent throughout the code, but if you use the option shown in the update, the configuration will apply only to that plot area. That is a behavior that I've observed here.