matplotlib multiple plots on one axis through loop does not update y_lim












0















I need to create a figure which contains multiple Gaussian histograms from a list of parameters (pairs of mu and sigma). What I have so far:



import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import numpy as np

def single_camel_plot(mu, sigma, ax):
x = np.linspace(mu - 4*sigma, mu + 4*sigma, 1000)
sns.lineplot(x, stats.norm.pdf(x, mu, sigma), ax=ax)
ax.set_ylim(bottom=0)

fig, ax = plt.subplots()
mu_sigma_pairs = [(16900, 2100), (18200, 1500)]
for mu, sigma in mu_sigma_pairs:
single_camel_plot(mu, sigma, ax)


The output is this figure.



figure with wrong y_lim



Ideally, the two histograms would use a single y-axis instead of a secondary axis, and the ylim should adjust to accomodate the tallest curve.










share|improve this question























  • Seems to me that the set_ylim(bottom=0) is the culprit. After commented it out it no longer creates the problem. It's strange that somehow the other parameter top is also affected by set_ylim although I did not specify a value for it.

    – Xiaoyu Lu
    Nov 13 '18 at 17:17











  • Setting the limit (even one) for your y-axis will turn off autoscaling. I don't think there's any way to autoscale only one end of the axis limits.

    – busybear
    Nov 13 '18 at 19:46
















0















I need to create a figure which contains multiple Gaussian histograms from a list of parameters (pairs of mu and sigma). What I have so far:



import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import numpy as np

def single_camel_plot(mu, sigma, ax):
x = np.linspace(mu - 4*sigma, mu + 4*sigma, 1000)
sns.lineplot(x, stats.norm.pdf(x, mu, sigma), ax=ax)
ax.set_ylim(bottom=0)

fig, ax = plt.subplots()
mu_sigma_pairs = [(16900, 2100), (18200, 1500)]
for mu, sigma in mu_sigma_pairs:
single_camel_plot(mu, sigma, ax)


The output is this figure.



figure with wrong y_lim



Ideally, the two histograms would use a single y-axis instead of a secondary axis, and the ylim should adjust to accomodate the tallest curve.










share|improve this question























  • Seems to me that the set_ylim(bottom=0) is the culprit. After commented it out it no longer creates the problem. It's strange that somehow the other parameter top is also affected by set_ylim although I did not specify a value for it.

    – Xiaoyu Lu
    Nov 13 '18 at 17:17











  • Setting the limit (even one) for your y-axis will turn off autoscaling. I don't think there's any way to autoscale only one end of the axis limits.

    – busybear
    Nov 13 '18 at 19:46














0












0








0








I need to create a figure which contains multiple Gaussian histograms from a list of parameters (pairs of mu and sigma). What I have so far:



import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import numpy as np

def single_camel_plot(mu, sigma, ax):
x = np.linspace(mu - 4*sigma, mu + 4*sigma, 1000)
sns.lineplot(x, stats.norm.pdf(x, mu, sigma), ax=ax)
ax.set_ylim(bottom=0)

fig, ax = plt.subplots()
mu_sigma_pairs = [(16900, 2100), (18200, 1500)]
for mu, sigma in mu_sigma_pairs:
single_camel_plot(mu, sigma, ax)


The output is this figure.



figure with wrong y_lim



Ideally, the two histograms would use a single y-axis instead of a secondary axis, and the ylim should adjust to accomodate the tallest curve.










share|improve this question














I need to create a figure which contains multiple Gaussian histograms from a list of parameters (pairs of mu and sigma). What I have so far:



import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import numpy as np

def single_camel_plot(mu, sigma, ax):
x = np.linspace(mu - 4*sigma, mu + 4*sigma, 1000)
sns.lineplot(x, stats.norm.pdf(x, mu, sigma), ax=ax)
ax.set_ylim(bottom=0)

fig, ax = plt.subplots()
mu_sigma_pairs = [(16900, 2100), (18200, 1500)]
for mu, sigma in mu_sigma_pairs:
single_camel_plot(mu, sigma, ax)


The output is this figure.



figure with wrong y_lim



Ideally, the two histograms would use a single y-axis instead of a secondary axis, and the ylim should adjust to accomodate the tallest curve.







python matplotlib seaborn






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 17:14









Xiaoyu LuXiaoyu Lu

431413




431413













  • Seems to me that the set_ylim(bottom=0) is the culprit. After commented it out it no longer creates the problem. It's strange that somehow the other parameter top is also affected by set_ylim although I did not specify a value for it.

    – Xiaoyu Lu
    Nov 13 '18 at 17:17











  • Setting the limit (even one) for your y-axis will turn off autoscaling. I don't think there's any way to autoscale only one end of the axis limits.

    – busybear
    Nov 13 '18 at 19:46



















  • Seems to me that the set_ylim(bottom=0) is the culprit. After commented it out it no longer creates the problem. It's strange that somehow the other parameter top is also affected by set_ylim although I did not specify a value for it.

    – Xiaoyu Lu
    Nov 13 '18 at 17:17











  • Setting the limit (even one) for your y-axis will turn off autoscaling. I don't think there's any way to autoscale only one end of the axis limits.

    – busybear
    Nov 13 '18 at 19:46

















Seems to me that the set_ylim(bottom=0) is the culprit. After commented it out it no longer creates the problem. It's strange that somehow the other parameter top is also affected by set_ylim although I did not specify a value for it.

– Xiaoyu Lu
Nov 13 '18 at 17:17





Seems to me that the set_ylim(bottom=0) is the culprit. After commented it out it no longer creates the problem. It's strange that somehow the other parameter top is also affected by set_ylim although I did not specify a value for it.

– Xiaoyu Lu
Nov 13 '18 at 17:17













Setting the limit (even one) for your y-axis will turn off autoscaling. I don't think there's any way to autoscale only one end of the axis limits.

– busybear
Nov 13 '18 at 19:46





Setting the limit (even one) for your y-axis will turn off autoscaling. I don't think there's any way to autoscale only one end of the axis limits.

– busybear
Nov 13 '18 at 19:46












1 Answer
1






active

oldest

votes


















1














You will need to set the limit outside the loop, such that it can autoscale when the plot is already filled.



import matplotlib.pyplot as plt
from scipy import stats
import numpy as np

def single_camel_plot(mu, sigma, ax):
x = np.linspace(mu - 4*sigma, mu + 4*sigma, 1000)
ax.plot(x, stats.norm.pdf(x, mu, sigma))

fig, ax = plt.subplots()
mu_sigma_pairs = [(16900, 2100), (18200, 1500)]
for mu, sigma in mu_sigma_pairs:
single_camel_plot(mu, sigma, ax)

ax.set_ylim(bottom=0) # <------- HERE

plt.show()





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286320%2fmatplotlib-multiple-plots-on-one-axis-through-loop-does-not-update-y-lim%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You will need to set the limit outside the loop, such that it can autoscale when the plot is already filled.



    import matplotlib.pyplot as plt
    from scipy import stats
    import numpy as np

    def single_camel_plot(mu, sigma, ax):
    x = np.linspace(mu - 4*sigma, mu + 4*sigma, 1000)
    ax.plot(x, stats.norm.pdf(x, mu, sigma))

    fig, ax = plt.subplots()
    mu_sigma_pairs = [(16900, 2100), (18200, 1500)]
    for mu, sigma in mu_sigma_pairs:
    single_camel_plot(mu, sigma, ax)

    ax.set_ylim(bottom=0) # <------- HERE

    plt.show()





    share|improve this answer




























      1














      You will need to set the limit outside the loop, such that it can autoscale when the plot is already filled.



      import matplotlib.pyplot as plt
      from scipy import stats
      import numpy as np

      def single_camel_plot(mu, sigma, ax):
      x = np.linspace(mu - 4*sigma, mu + 4*sigma, 1000)
      ax.plot(x, stats.norm.pdf(x, mu, sigma))

      fig, ax = plt.subplots()
      mu_sigma_pairs = [(16900, 2100), (18200, 1500)]
      for mu, sigma in mu_sigma_pairs:
      single_camel_plot(mu, sigma, ax)

      ax.set_ylim(bottom=0) # <------- HERE

      plt.show()





      share|improve this answer


























        1












        1








        1







        You will need to set the limit outside the loop, such that it can autoscale when the plot is already filled.



        import matplotlib.pyplot as plt
        from scipy import stats
        import numpy as np

        def single_camel_plot(mu, sigma, ax):
        x = np.linspace(mu - 4*sigma, mu + 4*sigma, 1000)
        ax.plot(x, stats.norm.pdf(x, mu, sigma))

        fig, ax = plt.subplots()
        mu_sigma_pairs = [(16900, 2100), (18200, 1500)]
        for mu, sigma in mu_sigma_pairs:
        single_camel_plot(mu, sigma, ax)

        ax.set_ylim(bottom=0) # <------- HERE

        plt.show()





        share|improve this answer













        You will need to set the limit outside the loop, such that it can autoscale when the plot is already filled.



        import matplotlib.pyplot as plt
        from scipy import stats
        import numpy as np

        def single_camel_plot(mu, sigma, ax):
        x = np.linspace(mu - 4*sigma, mu + 4*sigma, 1000)
        ax.plot(x, stats.norm.pdf(x, mu, sigma))

        fig, ax = plt.subplots()
        mu_sigma_pairs = [(16900, 2100), (18200, 1500)]
        for mu, sigma in mu_sigma_pairs:
        single_camel_plot(mu, sigma, ax)

        ax.set_ylim(bottom=0) # <------- HERE

        plt.show()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 20:33









        ImportanceOfBeingErnestImportanceOfBeingErnest

        129k12138215




        129k12138215






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286320%2fmatplotlib-multiple-plots-on-one-axis-through-loop-does-not-update-y-lim%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues