1

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()

1 Answers1

0

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()

enter image description here

aminrd
  • 4,300
  • 4
  • 23
  • 45
  • That seemed to help, thanks! I get an error now with my legend tho. "No handles with labels found to put in legend." Any recommendations? –  Mar 13 '21 at 22:03
  • @TamaraQawasmeh You're right. The problem came from `plt.legend()`. I updated the code, we should use `axis[0].legend()` instead. – aminrd Mar 13 '21 at 22:16
  • That worked. Is there any way to make adjustments to the dimensions of the graphs? Graph: https://ibb.co/hRshyzY [updated image link] –  Mar 13 '21 at 22:23
  • 1
    @TamaraQawasmeh I think for that you'd better to have another question, but I guess you can find related answer here: https://stackoverflow.com/questions/7965743/how-can-i-set-the-aspect-ratio-in-matplotlib – aminrd Mar 13 '21 at 22:30