简体   繁体   中英

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

I'm using matplotlib to create a density and blox plot but when I run my code, I get one graph with two plots overlapping each other. How can I restructure my code to output two separate individual graphs?

link to graph image: 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()

The cleanest way is to use side by side figures using 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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM