【问题标题】:Creating 2 plots but they're overlapping into one graph. How can I create 2 separate graphs using matplotlib?创建 2 个图,但它们重叠成一个图。如何使用 matplotlib 创建 2 个单独的图形?
【发布时间】:2021-06-11 14:09:18
【问题描述】:

我正在使用 matplotlib 创建一个密度和 blox 图,但是当我运行我的代码时,我得到一个图,其中两个图相互重叠。如何重组我的代码以输出两个单独的图表?

图表图片链接: https://ibb.co/6bCK9MZ

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

def make_t_distribution(sample_size, mean, sd):
    t_sample = stats.t.rvs(sample_size - 1, mean, sd, sample_size) # Random t-distribution sample
    sample_mean = np.mean(t_sample) # sample mean
    sample_std = np.std(t_sample) # sample standard deviation
    t_dist = stats.t(df = sample_size - 1, loc = sample_mean, scale = sample_std) # make a t-distribution based on the sample
    x_axis = np.linspace(t_dist.ppf(0.0001), t_dist.ppf(0.9999), 500) # Generate an x-axis based on t-quantile values
    
    return t_dist, x_axis

def make_prob_plot():
    
    ax = plt.axes()
    tdist1, x1=make_t_distribution(10,0,2)
    tdist2, x2=make_t_distribution(100,0,2)
    tdist3, x3=make_t_distribution(1000,0,2)
    tdist4, x4=make_t_distribution(10000,0,2)
    tdist5, x5=make_t_distribution(500,0,2)
    
    # density plot
    plt.xlim(-7.5,7.5)
    y1=ax.plot(x1,tdist1.pdf(x1), '-', label="$df=9$")
    y2=ax.plot(x2,tdist2.pdf(x2), ':', label="$df=99$")
    y3=ax.plot(x3,tdist3.pdf(x3), '--' ,label="$df=999$")
    y4=ax.plot(x4,tdist4.pdf(x4), '-.', label="$df=9999$")
    y5=ax.plot(x5,tdist5.pdf(x5), '.', label="$Normal$")
    plt.xlabel("Value")
    plt.ylabel("Density")
    plt.title("PDF Distribution Comparison $N(\mu=0$, $\sigma=2$)")
    plt.legend()
    
    # boxplot
    dist1 = np.random.normal(0,2,10)
    dist2 = np.random.normal(0,2,100)
    dist3 = np.random.normal(0,2,1000)
    dist4 = np.random.normal(0,2,10000)
    
    distributions = (dist1, dist2, dist3, dist4)
    plt.boxplot(distributions, labels = ("df=9","df=99","df=999","df=9999"));
    plt.boxplot(distributions, widths= .7);
    green_diamond = dict(markerfacecolor='g', marker='D')
    plt.boxplot(distributions, flierprops=green_diamond);
    
    
    return plt

make_prob_plot()

【问题讨论】:

    标签: python pandas numpy matplotlib scipy.stats


    【解决方案1】:

    最简洁的方法是使用plt.subplot 并排使用数字。

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    import seaborn as sns
    import scipy.stats as stats
    
    
    def make_t_distribution(sample_size, mean, sd):
        t_sample = stats.t.rvs(sample_size - 1, mean, sd, sample_size)  # Random t-distribution sample
        sample_mean = np.mean(t_sample)  # sample mean
        sample_std = np.std(t_sample)  # sample standard deviation
        t_dist = stats.t(df=sample_size - 1, loc=sample_mean, scale=sample_std)  # make a t-distribution based on the sample
        x_axis = np.linspace(t_dist.ppf(0.0001), t_dist.ppf(0.9999), 500)  # Generate an x-axis based on t-quantile values
    
        return t_dist, x_axis
    
    
    def make_prob_plot():
        figure, axis = plt.subplots(2,1)
        tdist1, x1 = make_t_distribution(10, 0, 2)
        tdist2, x2 = make_t_distribution(100, 0, 2)
        tdist3, x3 = make_t_distribution(1000, 0, 2)
        tdist4, x4 = make_t_distribution(10000, 0, 2)
        tdist5, x5 = make_t_distribution(500, 0, 2)
    
        # density plot
        plt.xlim(-7.5, 7.5)
        y1 = axis[0].plot(x1, tdist1.pdf(x1), '-', label="$df=9$")
        y2 = axis[0].plot(x2, tdist2.pdf(x2), ':', label="$df=99$")
        y3 = axis[0].plot(x3, tdist3.pdf(x3), '--', label="$df=999$")
        y4 = axis[0].plot(x4, tdist4.pdf(x4), '-.', label="$df=9999$")
        y5 = axis[0].plot(x5, tdist5.pdf(x5), '.', label="$Normal$")
        plt.xlabel("Value")
        plt.ylabel("Density")
        plt.title("PDF Distribution Comparison $N(\mu=0$, $\sigma=2$)")
        axis[0].legend()
    
        # boxplot
        dist1 = np.random.normal(0, 2, 10)
        dist2 = np.random.normal(0, 2, 100)
        dist3 = np.random.normal(0, 2, 1000)
        dist4 = np.random.normal(0, 2, 10000)
    
        distributions = (dist1, dist2, dist3, dist4)
        axis[1].boxplot(distributions, labels=("df=9", "df=99", "df=999", "df=9999"));
        axis[1].boxplot(distributions, widths=.7);
        green_diamond = dict(markerfacecolor='g', marker='D')
        axis[1].boxplot(distributions, flierprops=green_diamond);
    
        return plt
    
    
    make_prob_plot()
    

    【讨论】:

    • 这似乎有帮助,谢谢!我的传奇现在出错了。 “没有发现带有标签的把手放在图例中。”有什么建议吗?
    • @TamaraQawasmeh 你是对的。问题来自plt.legend()。我更新了代码,我们应该改用axis[0].legend()。
    • 那行得通。有什么办法可以调整图表的尺寸吗?图:ibb.co/hRshyzY[更新图片链接]
    • @TamaraQawasmeh 我认为你最好再问一个问题,但我想你可以在这里找到相关答案:stackoverflow.com/questions/7965743/…
    猜你喜欢
    • 2020-09-25
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2018-05-30
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多