创建 2 个图,但它们重叠成一个图。如何使用 matplotlib 创建 2 个单独的图形?

Creating 2 plots but they're overlapping into one graph. How can I create 2 separate graphs using matplotlib?

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

link 到图形图像: 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()

最简洁的方法是使用 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()